php::tmp 文件会保留多久?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4653224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php:: how long to tmp files stay?
提问by Hailwood
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.
如果用户上传了一个文件并且它已经存在,我想警告用户(这都是通过 ajax 完成的)并让他们选择替换它,或者取消它。
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.
而不是移动文件,我很好奇是否可以将文件保留在 tmp 中并在 ajax 响应中传回该文件的路径。
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.
如果他们的用户说覆盖该 ajax 请求中的旧文件,则将路径传递回 php,该 php 继续处理该文件。
For this to work however I need to know how long a file stays in php's tmp dir
为此,我需要知道文件在 php 的 tmp 目录中保留多长时间
回答by zerkms
Files uploaded through POST are deleted right after php script finishes its execution.
通过 POST 上传的文件在 php 脚本执行完成后立即被删除。
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."
根据php.net:“如果文件没有被移走或重命名,文件将在请求结束时从临时目录中删除。”
回答by netcoder
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 tempnamor tmpfile, or simply when writing to the temporary directory (see sys_get_temp_dir).
更一般地说,正如您的问题标题可能暗示的那样,临时文件夹会被系统清理干净。当使用诸如tempnam或tmpfile 之类的函数时,或者只是在写入临时目录时(请参阅sys_get_temp_dir),这是正确的。
In Ubuntu, this is done at every system reboot, or at a time interval, as defined in /etc/default/rcS
.
在 Ubuntu 中,这是在每次系统重新启动时或按时间间隔完成的,如/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).
在一些基于 Red Hat 的发行版中,它是使用tmpwatch
cronjob 中的实用程序完成的。在其他情况下,/tmp
分区是使用tmpfs
文件系统挂载的,这类似于 RAM 磁盘(因此在计算机关闭时会被清理)。
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.
另一种已知机制是大小阈值,这意味着临时目录在达到一定大小时将从旧文件中清除。
回答by Paul Feakins
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:
需要在 PHP 中设置三个变量以确保 /tmp 目录的垃圾收集正确发生,它们是:
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.
将 session.gc_maxlifetime 设置为您希望每个 tmp 文件在删除之前持续的秒数。如果您在 OpenCart 中登录到管理员,这是您将自动退出之前的秒数。例如,要设置半小时,您可以做 60 秒乘以 30 分钟,这将是 1800 秒的值。
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/
更多信息在这里:https: //www.antropy.co.uk/blog/opencart-php-session-tmp-files-filling-up/