kev wood Posted May 7, 2008 Share Posted May 7, 2008 i am trying to remove files from the server when a user enters onto a page on my web site. the user can upload images on to the server but when this page is entered i need the old image to be removed from the server. the code i am using looks like this $fileToRemove1 = 'image/thumbs/thumb_image1'; $exts = array('jpg', 'png', 'gif', 'jpeg'); foreach($exts as $ext) { if (file_exists($fileToRemove1.'.'.$ext)) { @chmod(0777, $fileToRemove1.'.'.$ext); } @unlink($fileToRemove1); } the directory is writeable so i dont think it is a problem with permissions. i have tested to see if it was just the images getting cached but this is not the case either. i deleted all the files from the server which had been uploaded, then uploaded a new image and it all worked fine the image was showing up where meant to. i then tried to upload a new image with the same file extension and it just showed the old image. i then upload an image with a different file extension and the first image continued to be displayed. i can find examples of how it works if the file extension is known but as mine is an image and can have three different file types then i need it to work with the extensions array so it can delete the image no matter what the extension. Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/ Share on other sites More sharing options...
saint959 Posted May 7, 2008 Share Posted May 7, 2008 Hi Kev, try removing the @ infront of unlink and see if you receive any errors? Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535082 Share on other sites More sharing options...
kev wood Posted May 7, 2008 Author Share Posted May 7, 2008 yes it comes back saying the is no such file directory. the actual wording is Warning: chmod(): No such file or directory in /home/acmeart/public_html/mercury/upload.php and it also says that there is no such file in the directory. the actual wording is unlink(image/thumbs/thumb_image1): No such file or directory Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535090 Share on other sites More sharing options...
saint959 Posted May 7, 2008 Share Posted May 7, 2008 hi, i think try something along the below. the unlink command needs to have the full location (i think). i have done this in a few places and i always use it in a similiar way to below. <?php $filename = "thumb_image1"; $exts = array('jpg', 'png', 'gif', 'jpeg'); foreach($exts as $ext) { if(file_exists("/home/acmeart/public_html/mercury/image/thumbs/$filename.".$ext)) { unlink("/home/acmeart/public_html/mercury/image/thumbs/$filename.".$ext); } } ?> Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535092 Share on other sites More sharing options...
DarkWater Posted May 7, 2008 Share Posted May 7, 2008 I see something wrong. This line: @unlink($fileToRemove1); You don't add the extension! Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535094 Share on other sites More sharing options...
saint959 Posted May 7, 2008 Share Posted May 7, 2008 lol, over looking the obvious sorry Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535096 Share on other sites More sharing options...
kev wood Posted May 7, 2008 Author Share Posted May 7, 2008 i have got the code to work now i modified to so it now looks like this <?php $path='image/thumbs/'; $fileToRemove1 = 'thumb_image1'; $exts = array('jpg', 'png', 'gif', 'jpeg'); foreach($exts as $ext) { if (file_exists($path.'/'.$fileToRemove1.'.'.$ext)) { unlink($path.'/'.$fileToRemove1.'.'.$ext); } } ?> i have moved the code on to its own page and used the include once function and it works fine it removes the file. the problem now is the cache it is displaying old images instead of the new ones that have been uploaded. i found this code to stop the caching of the images but it has not seemed to work <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535108 Share on other sites More sharing options...
saint959 Posted May 7, 2008 Share Posted May 7, 2008 hey, what i normally do to make sure the image does not get cached is put a randomn number after the image. i.e. <?php $rand = rand(1,3000); print "<img src=\"$image_url"?$rand\"/>"; ?> i see the quotes are not coming out here correctly. i hope you know what i mean Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535109 Share on other sites More sharing options...
kev wood Posted May 7, 2008 Author Share Posted May 7, 2008 sorry i took so long to get back my comp has been acting a fool. would this work for how i need it to. by this i mean this will have no effect on how the images are displayed and stored on the server? Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535158 Share on other sites More sharing options...
saint959 Posted May 7, 2008 Share Posted May 7, 2008 not at all. all that is going to do is add the random number after the image name thereby forcing it to reload each time. i have found that it works for me. Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535167 Share on other sites More sharing options...
kev wood Posted May 7, 2008 Author Share Posted May 7, 2008 should my code then look like this $rand=(1,300); // check if we have an image if ($image != "") { // ok we have the image $broad_img1='<img src="' . $path."/".$web_image_folder."/".$image . ?rand'" />'; } else { // error, we can't find the image. } not to sure about the quotes around everything. Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535170 Share on other sites More sharing options...
saint959 Posted May 7, 2008 Share Posted May 7, 2008 it looks right. just make sure of your quotes and then the $ infront of the rand within the image source i.e. $broad_img1='<img src=\"' . $path.'/'.$web_image_folder.'/'.$image . '?$rand\" />'; do you update the db using the $broad_img1 variable later in your script? Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535179 Share on other sites More sharing options...
kev wood Posted May 7, 2008 Author Share Posted May 7, 2008 no the $broad_img1 is used just for display purposes within the email that is sent out. i will change the code and see what it says to me. hopefully something along the lines of congratulations you are the winner. Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535184 Share on other sites More sharing options...
kev wood Posted May 7, 2008 Author Share Posted May 7, 2008 no it didnt work. it is giving me a error saying unexpected ? on that line. the image that is displayed on the email is uploaded by the user on the page before so the random number would have to go here then i would not no the full path to the image as it would have random number attached to it. Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535192 Share on other sites More sharing options...
saint959 Posted May 7, 2008 Share Posted May 7, 2008 here is the exact code as i use it on my sites. im getting pretty confused with all the quotes that get 'cleared' here <?php $rand = rand(1,3000); $photo = $_GET[photo]; print \"<img src=\"photos/$photo?$rand\"/><br/>"; ?> hope it helps! Link to comment https://forums.phpfreaks.com/topic/104533-delete-unlink-file/#findComment-535223 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.