Reputation: 12806
I have a php file which has the following path
Shubhmangalam/admin/welcome_image_edition/delete_image.php
and an image file with the following path
Shubhmangalam/welcome_images/image_1.jpg
I want to delete the image_1.jpg file which I know can be done by using unlink() method..
but the prob is that the parent folder of the .php
file and .jpg
file is different, and so is their level of file-system...and I cant find the proper way to get the path to delete the image_1.jpg file.
Now the code on the delete_image.php is accordingly
<?php
$image=$_REQUEST['image'];
if(unlink("./../welcome_images/".$image))
echo "Successfully Deleted";
else
echo "Wrong";
?>
Now the above is server-scripting code, I want to delete the image by getting appropriate path.. I dont want the actual path, but the path from the project folder that is Shubhmangalam
thanks in advance
Upvotes: 1
Views: 2379
Reputation: 197659
Shubhmangalam/admin/welcome_image_edition/delete_image.php
That is:
__FILE__
and an image file with the follwing path
Shubhmangalam/welcome_images/image_1.jpg
That is:
dirname(dirname(__DIR__)) . '/welcome_images/image_1.jp';
in the first file. Use the magic constants __FILE__
and __DIR__
for your own good, e.g.:
if (unlink(__DIR__."/../../welcome_images/".$image))
{
...
Upvotes: 2
Reputation: 360592
Be VERY careful with this type of code. You're not filtering the $_REQUEST value AT ALL, which allows a malicious user to specify ANY file on your server, e.g:
http://example.com/delete_image.php?image=../../../../../../../../../etc/passwd
You can fix your pathing problem by using an absolute path:
unlink('/path/leading/to/your/welcome_images/' . $image);
which relives you of the burden of having to figure out the appropriate relative path.
Upvotes: 2