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

Why Linux creates temp files without extension when you upload file with PHP, and Windows makes with extension?

我正在使用 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 上标记临时文件,但此功能并未真正公开给 end-user。文件可以用 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 及更高版本的兼容性 short-name。