简体   繁体   中英

php delete a file / check file existence

I'm having a small issue with pictures upload on my website. i'm allowing users to upload pictures, but it liiks like it takes some time for the server to actually execute the commands.

First it has to make sure no file with the same name already exists in the directory, and if it does it removes it with unlink ($pictureLocation); Then, it creates the new image with imagejpeg($dst_r, $pictureLocation,$jpeg_quality);

However, when I then try to see the picture (redirecting to the website), it displays the old one, the one that was supposedly removed! And it'll take a few refreshes for the correct picture to actually sho up. There's something I don't get here... :/

I'm having the same problem in local and on prod server, but the production server takes a much longer time to display the correct picture whereas one immediate refresh is enough in local.

I've checked the output of the unlink function and it' returns true so i'm guessing everything went fine.

I want to make sure I don't do my redirection before the process has been completed, otherwise users will think it didn't work... Any idea?

seems like it may be due to cache problem. try adding these lines to your html to prevent the page from cacheing

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>

also, if i were you, i will think of inserting the file with a new file name (may be line 29_05_01_07_30picture.png) rather than checking & updating in database.

move_uploaded_file($_FILES["file"]["name"],"upload/" .date("d_m_h_i_s"). $_FILES["file"]["name"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM