merylvingien Posted December 9, 2010 Share Posted December 9, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/221082-deleting-an-obsolete-file/ Share on other sites More sharing options...
BlueSkyIS Posted December 9, 2010 Share Posted December 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/221082-deleting-an-obsolete-file/#findComment-1144793 Share on other sites More sharing options...
merylvingien Posted December 9, 2010 Author Share Posted December 9, 2010 Thanks for the reply BlueSkyIS, sorry bout the fellas thing You are correct, when i do a is_file() check it says that its not a file, how can this be? I have tried the full path as well! Quote Link to comment https://forums.phpfreaks.com/topic/221082-deleting-an-obsolete-file/#findComment-1144886 Share on other sites More sharing options...
BlueSkyIS Posted December 9, 2010 Share Posted December 9, 2010 if the file exists but is_file() returns false the path given to is_file() is probably wrong. Quote Link to comment https://forums.phpfreaks.com/topic/221082-deleting-an-obsolete-file/#findComment-1144924 Share on other sites More sharing options...
merylvingien Posted December 9, 2010 Author Share Posted December 9, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/221082-deleting-an-obsolete-file/#findComment-1144932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.