Page 1 of 1

I cant delete an uploaded image by unlink function

Posted: Mon Nov 17, 2008 10:14 pm
by lukelee
when i upload a new image, I want the old image been deleted. I tried to use unlink, but it just not working. here is my code, can anyone tell me whats wrong with it? $imagedata is the file name passed from the previous page.

Code: Select all

 
<?php
require_once('db.php'); 
$address = $_POST[address];
$imagedata = $_POST[imagedata];
$path="upload/$imagedata";
$image1 = $_FILES['image1']['name'];
$new_name1 = "upload/" . md5(uniqid(rand(), true)) . substr($image1, strrpos($image1, "."));
move_uploaded_file($_FILES['image1']['tmp_name'], $new_name1);
$filesize1=$_FILES['image1']['size'];
if($filesize1!= 0)
{
    $image1 = basename($new_name1);
    $query = mysql_query("UPDATE image SET imagedata='$image1' where address='$address' && thumb='1'");
    unlink($path);
    echo "image has been changed, you will be redirecting to previous page in 3 seconds";
}
?>

Re: I cant delete an uploaded image by unlink function

Posted: Mon Nov 17, 2008 10:17 pm
by Eran
Check out the contents of $path, it might not be what you expect it to be. Also, check if its readable to the script -

Code: Select all

 
var_dump ( is_readable($path) );
 
Perhaps the proper permissions are not set?

Re: I cant delete an uploaded image by unlink function

Posted: Mon Nov 17, 2008 10:31 pm
by lukelee
ok, thanks, the output is bool(true).

Re: I cant delete an uploaded image by unlink function

Posted: Mon Nov 17, 2008 10:34 pm
by Eran
what is the result of the unlink? if you run

Code: Select all

 
$result = unlink($path);
var_dump($result);
 
What does it say?