php:: tmp 文件保留多长时间?

发布于 2024-10-11 11:51:50 字数 232 浏览 8 评论 0原文

我正在编写上传脚本。

如果用户上传一个文件并且该文件已经存在,我想警告用户(这都是通过ajax)并让他们选择替换它或取消。

而不是移动文件, 我很好奇是否可以将文件保留在 tmp 中并在 ajax 响应中传回该文件的路径。

如果用户说覆盖该 ajax 请求中的旧文件,则将路径传回 php,php 继续处理该文件。

为了这个工作,我需要知道文件在 php 的 tmp 目录中保留多长时间

I am working on an upload script.

If a user uploads a file and it already exists I want to warn the user (this is all through ajax) and give them the option to replace it, or cancel.

Instead of moving the file,
I was curious if I could just leave the file in tmp and pass back the path to that file in the ajax response.

If they user says overwrite the old file in that ajax request pass the path back to php which continues to work on the file.

For this to work however I need to know how long a file stays in php's tmp dir

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

违心° 2024-10-18 11:51:50

通过 POST 上传的文件将在 php 脚本执行完成后立即删除。

根据 php.net
“如果该文件尚未被移走或重命名,则该文件将在请求结束时从临时目录中删除。”

Files uploaded through POST are deleted right after php script finishes its execution.

According to php.net:
"The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed."

北方。的韩爷 2024-10-18 11:51:50

对于上传的文件,手册指出

该文件将从
末尾的临时目录
请求(如果尚未移走)
或重命名。

因此,应将要保留的文件移动到另一个位置。

更一般地说,正如您的问题标题可能暗示的那样,临时文件夹将由系统清理。使用 tempnamtmpfile,或者只是在写入临时目录时(请参阅sys_get_temp_dir)。

在 Ubuntu 中,这是在每次系统重新启动时或按照 /etc/default/rcS 中定义的时间间隔完成的。

在一些基于 Red Hat 的发行版中,这是使用 cronjob 中的 tmpwatch 实用程序完成的。在其他情况下,/tmp 分区使用 tmpfs 文件系统挂载,该文件系统类似于 RAM 磁盘(因此在计算机关闭时会被清理)。

另一种已知的机制是大小阈值,这意味着当临时目录达到一定大小时,将从旧文件中清除。

For uploaded files, the manual states:

The file will be deleted from the
temporary directory at the end of the
request if it has not been moved away
or renamed.

Files that are to be kept should therefore be moved to another location.

More generally, as your question title might imply, temporary folders are left to be cleaned up by the system. This is true when using functions like tempnam or tmpfile, or simply when writing to the temporary directory (see sys_get_temp_dir).

In Ubuntu, this is done at every system reboot, or at a time interval, as defined in /etc/default/rcS.

In some Red Hat based distros, it is done using the tmpwatch utility from a cronjob. In others, the /tmp partition is mounted using the tmpfs filesystem, which is similar to a RAM disk (therefore being cleaned when the computer shuts down).

Another known mechanism is a size threshold, which means that the temporary directory will be cleaned up from the older files when it reaches a certain size.

仙气飘飘 2024-10-18 11:51:50

需要在 PHP 中设置三个变量,以确保 /tmp 目录的垃圾收集正确发生,它们是:

session.gc_maxlifetime = 21600
session.gc_probability = 1
session.gc_divisor = 100

将 session.gc_maxlifetime 设置为您希望每个 tmp 文件在被删除之前持续的秒数。如果您登录 OpenCart 中的管理员,这是您自动注销之前的秒数。例如,要设置半小时,您需要执行 60 秒乘以 30 分钟,即得到 1800 秒的值。

另外两个变量与垃圾收集器的运行时间有关,如果您遇到问题,请将它们设置为上面的值非常重要。

更多信息请点击这里:
https://www.antropy.co .uk/blog/opencart-php-session-tmp-files-filling-up/

There are three variables that need to be set in PHP to make sure that Garbage Collection of the /tmp directory happens correctly and they are:

session.gc_maxlifetime = 21600
session.gc_probability = 1
session.gc_divisor = 100

Set session.gc_maxlifetime to be the number of seconds you want each tmp file to last before it's deleted. If you login to the admin in OpenCart, this is the number of seconds until you will automatically be logged out. For example to set half an hour, you would do 60 seconds times 30 minutes which would be a value of 1800 seconds.

The other two variables are related to when the Garbage Collector will run, and it's important that they are set to the values above if you're having problems with this.

More info here:
https://www.antropy.co.uk/blog/opencart-php-session-tmp-files-filling-up/

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