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
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
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
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
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
Share on other sites

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
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
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
Share on other sites

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
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
Share on other sites

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