In PHP, tmpfile() creates a temporary file and returns an open file handle to it. The handle is opened in read and write mode.
The file is removed automatically when you close it with fclose() or when the script ends. This makes it a safe place to keep short lived data without worrying about cleanup.
Syntax
Call tmpfile() with the values you need. Start with this line.
PHP
$handle = tmpfile();
Call
What it does
tmpfile()
Creates a temporary file and returns a handle you can read and write.
stream_get_meta_data(tmpfile())['uri']
Shows the real path of the temporary file.
fclose($handle)
Closes the handle, and PHP deletes the temporary file.
tmpfile() takes no arguments. The file is created in the system temp folder with permissions 0600. If you need to know the path, read the uri value from stream_get_meta_data().
Library declarations
PHP
tmpfile(): resource|false
Parameters
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
resource|false
Returns a file handle for the new temporary file, or false on failure.
PHP Examples
Example 1 — Write and Read Back
Write text into a temporary file, rewind, and read it again.