简体   繁体   English

php删除文件/检查文件是否存在

[英]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); 首先,它必须确保目录中不存在具有相同名称的文件,如果这样做,则使用unlink($ pictureLocation)将其删除; Then, it creates the new image with imagejpeg($dst_r, $pictureLocation,$jpeg_quality); 然后,它使用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. 我检查了unlink函数的输出,它返回true,所以我猜一切都很好。

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 尝试将这些行添加到html中以防止页面缓存

<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. 另外,如果我是您,我会考虑使用新文件名插入文件(可能是29_05_01_07_30picture.png行),而不是在数据库中进行检查和更新。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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