PHP:: 如何在读取文件后从服务器删除文件。 Unlink() 之前执行
我试图在第一次显示后从服务器删除图片(.jpg)。 但文件在显示之前就被删除了(unlink();)。我已经尝试过 sleep() 但这只会延迟加载,并且在显示之前删除所有文件。
I'm trying to delete a picture (.jpg) from server after the first time showed.
But the file is deleted (unlink();) before showed. I've already tried with sleep() but this only delay the loading and after all the file is deleted before showed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 mod_rewrite 将 jpg 请求重定向到脚本,该脚本将图像加载到内存中,删除文件,然后提供图像。 IMO,这是最简单、最容易的解决方案。下面的不安全示例...
示例 .htaccess 文件:
index.php
You could use mod_rewrite to redirect jpg requests to a script that loads the image into memory, deletes the file, then serves up the image. IMO, this is the simplest and easiest solution. Unsafe example below...
Example .htaccess file:
index.php
恐怕我能做的就是尽量避免这种含糊不清的情况。
Best I can do with the vagueness, I'm afraid.
这是因为所有 PHP 在渲染输出之前都在堆栈中执行。您需要设置一个函数来标记文件以在下一页加载时删除。
或者您可以设置一些 AJAX 在文档加载后删除图片。
This is because all PHP is executed in the stack prior to rendering the output. You will need to set up a function to flag the file for deletion on the next page load.
OR you can set up some AJAX to delete the picture after the document has loaded.