tempnam()
tempnam() — Create file with unique file name.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Creates a file with a unique filename, with access permission set to 0600, in the specified directory. If the directory does not exist or is not writable, tempnam() may generate a file in the system's temporary directory, and return the full path to that file, including its name.
Signature
tempnam(string $directory, string $prefix): string|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $directory | string | The directory where the temporary filename will be created. |
| $prefix | string | The prefix of the generated temporary filename. |
Return value
Returns the new temporary filename (with path), or false on failure.
Example
A runnable snippet:
$tmpfname = tempnam("/tmp", "FOO"); $handle = fopen($tmpfname, "w"); fwrite($handle, "writing to tempfile"); fclose($handle); // do something here unlink($tmpfname);
Changelog
| Version | Change |
|---|---|
| 8.4.0 | The name of files created by tempnam() are now 13 bytes longer. The total length is still platform-dependent. |
| 7.1.0 | tempnam() now emits a notice when falling back to the temp directory of the system. |
See also
tmpfile(), sys_get_temp_dir(), unlink()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page tempnam() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.