如何创建临时上传文件并在一段时间后删除它
是否可以将 move_uploaded_file
上传的文件存储到某个目录并标记为几分钟后删除?我试图让一个临时文件在一段时间后保留在服务器上,然后它将被删除(仅当它未被处理时),因此当文件已上传并移动到目录时,如果它尚未被处理(不要担心这部分,这将由我完成)它应该从服务器中删除该文件。
您无法将临时文件保留在服务器上(不使用 move_uploaded_file
),因此基本上存储在服务器上的 tmp
目录中,我如何在上传时保留文件进入临时目录(或移动到一个目录)并“标记”它以便稍后删除(如果不处理)。
我试图研究这是如何可能的,但我没有找到任何类型的关于如何创建临时文件以在一定时间后保留的问题/主题。
编辑:澄清一下,我不希望在上传后脚本执行完毕后删除文件。
Is it possible to store the uploaded file by move_uploaded_file
to a directory and mark it to be deleted after a few minutes? I'm trying to make a temporary file stay on the server after a period of time then it will get deleted (only if it's not processed with) so when a file has been uploaded and moved to a directory if it has not been processed (don't worry about this part, this will be done by me) it should delete the file from the server.
There is no way you can keep the temporary file on the server (without using move_uploaded_file
) so basically stored in the tmp
directory on the server how can I keep the file when uploaded into the temporary directory (or moved to a directory) and "mark" it to be deleted later on if not processed.
I've tried to research how this is possible, but I have not found any type of questions/topics about how to create a temporary file to stay after a limit of time.
EDIT: To clarify, I do not want the file deleted after the script has finished executing after upload.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请按照下列步骤操作:
首先使用常用的 move_upload_file 函数并将文件保留在您的服务器上。
现在创建一个新的 php 程序来执行此操作:
现在每天将上述程序作为 cron 运行一次。
Follow these steps:
First use the usual move_upload_file function and keep the file on your server.
Now create a new php program to do this:
Now run the above program as a cron once a day.
没有自动的方法。有些服务器会在一段时间后定期清除 /tmp 目录中的文件,但唯一可以确定的方法是编写自己的清理脚本并作为 cron 作业运行。 (或者使用穷人的 cron 并让脚本触发清理检查,这是一种不太可靠但有时可行的方法。)
您可以标记数据库或文本文件中文件的过期时间,或者简单地使用文件系统时间戳来检查是否有过期的文件。
一个更复杂的解决方案是将文件移动到某种缓存中,例如 Redis,您可以在其中设置过期时间 (TTL),但这取决于您存储的数据类型,并且是一个有点深奥的解决方案。
There is no automatic way. Some servers regularly clear out the files in the /tmp directory after a certain period of time, but the only way you could be sure is if you wrote your own cleansing script and ran as a cron job. (Or use the poor-man's cron and have your scripts trigger the cleanse check, a less reliable but sometimes viable method.)
You can either flag the expiration time for the files in a database or text file, or simply use the filesystem timestamp to check for expired files.
A more complex solution would be to move the file into some kind of cache like Redis where you can set an expiration time (TTL), but that would depend on what kind of data you are storing and is a somewhat esoteric solution.
我有另一种方法。我将文件移动到临时位置。处理后不会在固定时间内移除。相反,下次脚本运行时,它将删除所有以前上传和处理的文件。这避免了 cron 作业的要求。消除一点头痛。
I have another method that I use. I move the file to temp location. After it is processed it will not be removed on a fixed time. Rather next time the script runs, it will remove all previously uploaded and processed files. This avoids the requirement of cron jobs. Eliminating a little head ache.