Forum Moderators: coopster

Message Too Old, No Replies

unlink() not working

Is there a way to use sudo in php?

         

emptycrawford

3:42 am on Dec 9, 2008 (gmt 0)

10+ Year Member



Well, this is my first thread, so please, be nice. Basically, I have an image upload system that creates thumbnail images, stores the original and thumbnails, and puts the links to the files in a mysql database. However, I'd like to be able to delete all of the above references all at once with php. Below is the code with which I am working.

$prefix = "http://www.mydomain.com";

$query = "select * from `uploads` where id = " . $id;
// $id is passed to the page via a link
$result = mysql_query($query);

$row = mysql_fetch_array($result);

$i = 0;

$file = fopen($prefix . $row['image'],'w')
or die("Could not open file.");
fclose($file);
if(unlink(($prefix . $row['image']))) $i++;

$file = fopen($prefix . $row['thumb_small'],'w')
or die("could not open file.<br />");
$fclose($file);
if(unlink($prefix . $row['thumb_small'])) $i++;

$file = fopen($prefix . $row['thumb_large'],'w')
or die("could not open file");
$fclose($file);
if(unlink($prefix . $row['thumb_large'])) $i++;

if($i == 2)
{
mysql_free_result($result);

$query = "delete from `uploads` where id = " . $id;
mysql_query($query) or die(mysql_error());

header("Location:edit.php?deleted=$i");
}
else
{
echo (3-$i) . " files were not deleted.";
}

My results are three lines of "Could not delete file" and then "3 files were not deleted". I know I have rwx rights to the files because php wrote the files to the folder in the first place and after storing the files, I call chmod('file', 0777).

Any help is greatly appreciated.

omoutop

11:15 am on Dec 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You missed a trailing slash at end of $prefix

$file = fopen($prefix ."/". $row['image'],'w');
Change he rest 2 accordingly

emptycrawford

12:54 pm on Dec 9, 2008 (gmt 0)

10+ Year Member



Oh, sorry, the db links have a slash at the beginning. I forgot to specify. So, "$prefix . $row['image']" is something like 'http://www.mydomain.com/somefolder/uploads/something.jpg'.

andrewsmd

7:39 pm on Dec 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why are you opening the files if you are trying to remove them. Also, try a clearstatcache(); after each fclose. The last thing I would say is to var_dump($prefix) after each unlink and paste that path to make sure it is valid and it is not some weird type.