Jump to content

delete unlink file


kev wood

Recommended Posts

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

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

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

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

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

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

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

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

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.