使用 /tmp 目录中的文件
Working with files inside /tmp directory
我正在尝试验证上传的 .zip 文件的内容,然后将其保存到所需目录。
我的问题是:我可以重命名、提取 tmp/
目录中的文件吗?文件会自动删除吗?
我只想知道我自己在 tmp/
目录中创建的文件发生了什么。
当前进程为:
- 将
tmp_name
重命名为 ...['tmp_name'].zip (if not exists)
- 删除
...['tmp_name']
个文件。
- 将 zip 解压缩到文件夹名称
...['tmp_name']
,文件已被删除。
- 删除
.zip
个文件
- 验证文件夹内的内容。
- 移动或删除提取的文件夹。
这取决于您的操作系统和 PHP 配置。 PHP 不管理文件系统,任何应用程序都可以添加此目录。
来自 http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir :
upload_tmp_dir string The temporary directory used for storing files
when doing file upload. Must be writable by whatever user PHP is
running as. If not specified PHP will use the system's default.
If the directory specified here is not writable, PHP falls back to the
system default temporary directory. If open_basedir is on, then the
system default directory must be allowed for an upload to succeed.
简而言之,这取决于您的操作系统和配置,因为这只是指向系统中某处文件夹的指针 -- PHP 不会为您管理。许多只会在启动时删除文件,因此您应该在解压缩后小心清理(或使用一些东西清理任何垃圾)。
如果创建库或模块供一般重复使用,那么您需要在文档中指定您使用的临时文件夹的要求——当然,如果您将它用于快速处理以外的任何事情。 (也就是说,你可能会只写代码然后忘记它,但如果你想安全起见......)。
如你所见here:
On most systems, this directory is cleared out at boot or at shutdown by the local system.
但正如文档所述,在 Linux 的级别上没有太多指定,这取决于分布。大多数服务器不会经常重启,因此 /tmp
文件夹可能会变得一团糟,占用一些(大量)磁盘空间。然而,大多数 Linux 系统允许设置 TMPTIME
变量:文件可以在 /tmp
中存活的天数。因此,通过将变量设置为 1
超过一天的文件,将被自动删除(尽管可能不会在它们变成一天后一秒钟)。有关详细信息,请参阅 this answer。
简答:文件是文件系统的一部分。它们通常不会自动删除,只有在重新启动时或它们足够旧时才会删除。这是为了确保当您必须对文件执行一些操作时,它们不会中途消失。虽然当然没有人完全知道。
因此,您应该确保为文件指定不同的名称:如果两个进程同时发生,则它们不应交互。例如,使用一个随机数,以便第一个文件被命名为 zip151323.zip
,第二个文件被命名为 zip4745443233.zip
.
我正在尝试验证上传的 .zip 文件的内容,然后将其保存到所需目录。
我的问题是:我可以重命名、提取 tmp/
目录中的文件吗?文件会自动删除吗?
我只想知道我自己在 tmp/
目录中创建的文件发生了什么。
当前进程为:
- 将
tmp_name
重命名为...['tmp_name'].zip (if not exists)
- 删除
...['tmp_name']
个文件。 - 将 zip 解压缩到文件夹名称
...['tmp_name']
,文件已被删除。 - 删除
.zip
个文件 - 验证文件夹内的内容。
- 移动或删除提取的文件夹。
这取决于您的操作系统和 PHP 配置。 PHP 不管理文件系统,任何应用程序都可以添加此目录。
来自 http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir :
upload_tmp_dir string The temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default.
If the directory specified here is not writable, PHP falls back to the system default temporary directory. If open_basedir is on, then the system default directory must be allowed for an upload to succeed.
简而言之,这取决于您的操作系统和配置,因为这只是指向系统中某处文件夹的指针 -- PHP 不会为您管理。许多只会在启动时删除文件,因此您应该在解压缩后小心清理(或使用一些东西清理任何垃圾)。
如果创建库或模块供一般重复使用,那么您需要在文档中指定您使用的临时文件夹的要求——当然,如果您将它用于快速处理以外的任何事情。 (也就是说,你可能会只写代码然后忘记它,但如果你想安全起见......)。
如你所见here:
On most systems, this directory is cleared out at boot or at shutdown by the local system.
但正如文档所述,在 Linux 的级别上没有太多指定,这取决于分布。大多数服务器不会经常重启,因此 /tmp
文件夹可能会变得一团糟,占用一些(大量)磁盘空间。然而,大多数 Linux 系统允许设置 TMPTIME
变量:文件可以在 /tmp
中存活的天数。因此,通过将变量设置为 1
超过一天的文件,将被自动删除(尽管可能不会在它们变成一天后一秒钟)。有关详细信息,请参阅 this answer。
简答:文件是文件系统的一部分。它们通常不会自动删除,只有在重新启动时或它们足够旧时才会删除。这是为了确保当您必须对文件执行一些操作时,它们不会中途消失。虽然当然没有人完全知道。
因此,您应该确保为文件指定不同的名称:如果两个进程同时发生,则它们不应交互。例如,使用一个随机数,以便第一个文件被命名为 zip151323.zip
,第二个文件被命名为 zip4745443233.zip
.