php - 为什么使用 PHP 上传文件时 Linux 会创建没有扩展名的临时文件,而 Windows 会创建带有扩展名的临时文件?

标签 php linux windows temp

我将 PHP 7.0 和 7.2 与 UBUNTU 18.0 TLS 和 Windows 10 一起使用,

我有一个通常上传文件并处理其内容的脚本,

我要问的行为是,在 Ubuntu 上,为上传文件创建的临时文件是没有扩展名的,如下所示:

path: "/tmp"
filename: "phpdvdVPB"
basename: "phpdvdVPB"
pathname: "/tmp/phpdvdVPB"
extension: ""

但在 Windows 上,创建的临时文件的扩展名如下:

"dirname" => "C:\xampp\tmp"
"basename" => "php34E6.tmp"
"extension" => "tmp"
"filename" => "php34E6"

当您使用 tempnam() PHP 方法创建临时文件时,会发生相同的结果,它将在 Linux 上创建不带扩展名的文件,但 Windows 上会有扩展名。

所以我的问题是,为什么 Linux 创建没有扩展名的临时文件,而 Windows 会创建临时文件?

最佳答案

.TMP 在 Windows 上没有编程意义,扩展名通常未注册。然而,临时文件具有此扩展名是一种常见约定,以便用户知道这些文件可以安全删除。

DOS 和旧的 Windows 版本不支持在最后一个句柄关闭时自动删除文件,因此如果应用程序崩溃,它可能无法删除其临时文件。

PHP 可能正在使用 GetTempFileName功能会自动为您添加此扩展。此函数负责为您创建一个唯一的文件名。

实际上有一种方法可以在 Windows 上标记临时文件,但此功能并未真正向最终用户公开。可以使用 FILE_ATTRIBUTE_TEMPORARY 属性来标记文件。这告诉 Windows 不要将内容写入磁盘,除非内存管理器在分页期间需要。

我没有规范的文档引用说所有 .TMP 文件都是临时文件,这是旧历史并且按照惯例完成。

除了使用此扩展的 GetTempFileName 函数之外,KB 92635说:

A number of files may appear on the hard drive in various directories beginning with a tilde character (~) and ending with a .TMP extension. These may be temporary files created by Windows that remain on the hard drive due to an irregular exit from a Windows session.

Under normal conditions, these files are closed and deleted by Windows when you quit a Windows session. However, if you quit Windows in an irregular way (for example, restarting the computer or turning it off during an active Windows session) the files are not closed or deleted.

...

To delete a temporary file, use the following steps:

...

Delete any existing .TMP files. Make sure Windows is not running at the time these files are deleted. Some of these .TMP files may be files that Windows is using.

本文档针对 Windows 2 和 3。~ 前缀是可选的,~ 用于 Windows 95 及更高版本上的兼容性短名称。

关于php - 为什么使用 PHP 上传文件时 Linux 会创建没有扩展名的临时文件,而 Windows 会创建带有扩展名的临时文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56272480/

相关文章:

linux - 无法理解 fork() 描述

Python socket.accept() 没有收到任何连接请求

windows - 在 Anaconda Powershell 中只获取 IP 地址

php - 创建没有多个 SQL 查询的 JSON 对象 (PHP)

php - 一次为每次点击生成一个唯一的 ID

php - PHP图像识别

linux - 在 Cygwin 上安装 pciutils 和 makefile 错误

python - 如何在 Windows 上隐藏文件?

windows - 如何在 PowerShell 中输出一些东西

php - 未找到基表或 View :1146 in symfony3