Search code examples
phplinuxwindowstemp

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


I'm using PHP 7.0 and 7.2 with both UBUNTU 18.0 TLS and Windows 10,

I have a script that normally uploads files and process its content,

the behavior I'm asking about is that on Ubuntu, the temp file that created for the uploaded file is created without an extension like the below :

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

but on Windows, the created temp file is made with an extension like :

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

And the same result happens when you use tempnam() PHP method that creates temp file, it will create the file without extension on Linux, but Windows will have the extension.

So my question is, why Linux creates temp files without extensions, and Windows yes?


Solution

  • .TMP has no programmatic meaning on Windows, the extension is usually not registered. It is however a common convention for temporary files to have this extension so that users know that these files are safe to delete.

    DOS and old Windows versions do not support automatic deletion of files when the last handle is closed so if an application crashes it might be unable to delete its temporary files.

    PHP is probably using the GetTempFileName function which adds this extension for you automatically. This function takes care of creating a unique filename for you.

    There is actually a way to mark temporary files on Windows but this functionality is not really exposed to the end-user. Files can be marked with the FILE_ATTRIBUTE_TEMPORARY attribute. This tells Windows to not write the content to disk unless required by the memory manager during paging.

    I don't have a canonical documentation reference that says that all .TMP files are temp files, this is old history and done by convention.

    In addition to the GetTempFileName function using this extension, KB 92635 says:

    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.

    This document is aimed at Windows 2 and 3. The ~ prefix is optional and ~ is used in the compatibility short-name on Windows 95 and later.