如何创建临时上传文件并在一段时间后删除它

发布于 2024-11-15 11:41:55 字数 422 浏览 1 评论 0原文

是否可以将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拿命拼未来 2024-11-22 11:41:55

请按照下列步骤操作:

首先使用常用的 move_upload_file 函数并将文件保留在您的服务器上。

现在创建一个新的 php 程序来执行此操作:

  1. 使用 filetime 函数确定文件的上次编辑时间。因此您可以检查它是否是最近处理的。
  2. 将上面的日期/时间与您的删除标准进行比较。
  3. 使用 php unlink 函数删除旧文件。

现在每天将上述程序作为 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:

  1. use the filetime function to determine the last edited time of the file. Thus you can check if it was processed recently.
  2. Compare the date/time above with your deletion criteria.
  3. Use the php unlink function to get rid of the older files.

Now run the above program as a cron once a day.

情绪操控生活 2024-11-22 11:41:55

没有自动的方法。有些服务器会在一段时间后定期清除 /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.

北座城市 2024-11-22 11:41:55

我有另一种方法。我将文件移动到临时位置。处理后不会在固定时间内移除。相反,下次脚本运行时,它将删除所有以前上传和处理的文件。这避免了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文