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
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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.