I cant delete an uploaded image by unlink function

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
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

I cant delete an uploaded image by unlink function

Post 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";
}
?>
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: I cant delete an uploaded image by unlink function

Post 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?
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

Re: I cant delete an uploaded image by unlink function

Post by lukelee »

ok, thanks, the output is bool(true).
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: I cant delete an uploaded image by unlink function

Post 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?
Post Reply