Jump to content

Deleting an obsolete file


merylvingien

Recommended Posts

Hi fellas, i am wondering if someone can point me in the right direction?

 

I have a simple upload script where users can upload thier profile picture, but if they want to change that picture i would like to delete the old one, to save space and time cleaning up the left overs.

 

So, without further ado, i gather the info from the existing database:

 

$existingimage = "/profileimage/". $userid2['image'];

 

Then further on in the script when it comes time to add or update the image i have:

 

			if (!empty($existingimage)) {unlink($existingimage);}
			$insert= ("UPDATE userdatabase SET image='$finalname' WHERE user='$userid'");
			mysql_query($insert);

 

Only problem is, the unlink part dont work, the original image is still there!

Any clues?

Link to comment
https://forums.phpfreaks.com/topic/221082-deleting-an-obsolete-file/
Share on other sites

fellas is a pretty big assumption.

 

i would try:

1. use unlink() with an absolute path instead of a relative path.

2. use chmod() to change file permissions before using unlink().

3. use chmod() to set permissions on the file after the upload, so you can use unlink() on it later.

 

also, you check whether $existingimage is empty, but i bet if you checked whether $existingimage is_file(), you might find that it isn't, thus idea 1 might make all the difference.

I am just going through everything now, this is being done on my local server/computer rather than on a hosted server. I thought that may make a difference but i cant find any reference.

 

I have echoed out the full path

 

$userimage= $userid2['image'];
$existingimage = "http://127.0.0.1/test/profileimage/$userimage";

if(file_exists($existingimage)){
    unlink($existingimage);
    } else {echo"file doesnt exist $existingimage"; exit();}

 

Then copied the path into a new window and the image is shown.

Cant make any sense of it at all...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.