Can't Delete an image file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

Can't Delete an image file

Post by chopWood »

Well, here's the code:

Code: Select all

function delete_file($which_one) {
if (unlink($which_one)) {
echo "$which_one deleted.";
} else {
echo "$which_one: delete failed.";
}
}
Here's the error:

Warning: unlink( photos/butterflyMilkweedweb.jpg ) [function.unlink]: No such file or directory in /ApacheServer/ApacheDocRoot/sites_in_progress/crc/view_photos.php on line 16
photos/butterflyMilkweedweb.jpg : delete failed.

I checked and found that there IS a directory "photos" on the same level as the file view_photos.php.
In that directory is butterflyMilkweedweb.jpg.
So, the path "photos/butterflyMilkweedweb.jpg" should be correct.
This also does not work: "/photos/butterflyMilkweedweb.jpg"
... something I'm not seeing here.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Can't Delete an image file

Post by social_experiment »

You might want to take a look at file_exists() to check for the file before trying to delete it.

It could be an issue with file permissions;
http://stackoverflow.com/questions/7121 ... te-my-file
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can't Delete an image file

Post by requinix »

Also note that file paths are relative to the current working directory. This is not always the same directory your script is in. For example, if ./a.php includes dir/b.php then the working directory will be ./ - even inside dir/b.php.

So if you have some file that

Code: Select all

include "crc/view_photos.php";
and view_photos.php wants to delete photos/butterflyMilkweedweb.jpg, you actually have to give "crc/photos/butterflyMilkweedweb.jpg". But that requires knowing what the current directory is.

That's easy, actually, but there's a simpler way: use __DIR__ or $_SERVER["DOCUMENT_ROOT"]. Those always point to the same place, and they're absolute paths so they won't be affected by the current working directory.

Code: Select all

unlink(__DIR__ . "/photos/butterflyMilkweedweb.jpg"); // relative to the directory this script is in
unlink($_SERVER["DOCUMENT_ROOT"] . "/crc/photos/butterflyMilkweedweb.jpg"); // relative to the document root (assuming it's sites_in_progress/)
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

Re: Can't Delete an image file

Post by chopWood »

Thank you for your help. A faulty path name is what I thought had to be but careful examination didn't reveal it to me as the path names of those files to be deleted seemed to match what was in the variable. However, there is always the unseen that needs to be checked if all else fails so I tried trimming each variable in the array before using it as the path name of the file to be deleted. That worked.

I wasn't sure how a space worked its way into the pathname in the file reading code:

while (true == ($entry = $d->read())) {
if ($entry !== "." && $entry !== ".."){
$entry = "photos/".$entry;
//etc


however this line (I just picked up on it), seems pretty suspect to me:

echo '<td> delete this image:<input name="imageNames[]" value = " '.$entry.' " type="checkbox"/><br />
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Can't Delete an image file

Post by Mordred »

It looks like you'd allow anyone to give you filenames in imageNames and you'll blindly delete them. What measures have you taken against that?
Post Reply