PHP tempnam() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, tempnam() creates a new empty file with a unique name in a folder you choose and returns the full path.

It is a safe way to get a temporary file because PHP makes sure the name is not used yet. The file is created for you, but it is not deleted automatically, so you remove it with unlink() when you are done.

Syntax

Call tempnam() with the values you need. Start with this line.

PHP
$file = tempnam(sys_get_temp_dir(), 'demo');
CallWhat it does
tempnam(sys_get_temp_dir(), 'demo')Creates a file such as /tmp/demoA1b2C3 in the system temp folder.
tempnam(__DIR__ . '/cache', 'tmp_')Creates a temporary file inside your own cache folder.
tempnam('/no/such/folder', 'x')Falls back to the system temp folder if the folder does not exist.

The file is created with permissions 0600, so only the owner can read and write it. The prefix is limited to a few characters on some systems. Use sys_get_temp_dir() to find the right temp folder on any server.

Library declarations
PHP
tempnam(string $directory, string $prefix): string|false

Parameters

ParameterDescription
directoryRequired. The folder where the file should be created. If it does not exist, PHP falls back to the system temp folder.
prefixRequired. The start of the file name. PHP adds random characters after it.

Return Value

Return ValueDescription
string|falseReturns the full path of the new file, or false on failure.

PHP Examples

Example 1 — Create a Temporary File

Ask PHP for a unique file name in the temp folder and check what was created.

PHP
<?php
$file = tempnam(sys_get_temp_dir(), 'demo');

echo str_starts_with(basename($file), 'demo') ? 'Name starts with demo' : 'Other name', PHP_EOL;
echo 'File exists: ', file_exists($file) ? 'yes' : 'no', PHP_EOL;
echo 'Size: ', filesize($file), ' bytes', PHP_EOL;

unlink($file);

How It Works

  1. tempnam() creates an empty file with a unique name that begins with demo.
  2. basename() removes the folder part so we can test the start of the name.
  3. The file already exists on disk but has no content yet, so its size is 0.
  4. unlink() deletes the file because PHP does not do it for you.

Example 2 — A Folder That Does Not Exist

When the folder is missing, tempnam() uses the system temp folder instead of failing.

PHP
<?php
$file = @tempnam('/no/such/folder', 'demo');

if ($file === false) {
    echo 'Could not create a file', PHP_EOL;
} elseif (dirname($file) === rtrim(sys_get_temp_dir(), '/\\')) {
    echo 'Fell back to the system temp folder', PHP_EOL;
    unlink($file);
}

How It Works

  1. The folder /no/such/folder does not exist.
  2. tempnam() quietly uses the system temp folder instead, and @ hides the notice PHP raises.
  3. dirname() gives the folder of the new file, which we compare with sys_get_temp_dir().
  4. Because they match, the script prints the fallback message and deletes the file.

Example 3 — Write, Read and Clean Up

Use a temporary file to hold data for a moment, then remove it. Two calls also give two different names.

PHP
<?php
$first  = tempnam(sys_get_temp_dir(), 'tmp');
$second = tempnam(sys_get_temp_dir(), 'tmp');

file_put_contents($first, 'temporary data');

echo file_get_contents($first), PHP_EOL;
echo $first !== $second ? 'Names are different' : 'Same name', PHP_EOL;

unlink($first);
unlink($second);

How It Works

  1. Each tempnam() call creates its own empty file with a unique name.
  2. file_put_contents() fills the first file and file_get_contents() reads it back.
  3. The two names are never the same, which is why tempnam() is safe to use.
  4. Both files are deleted at the end so nothing is left behind.

Frequently Asked Questions

It creates a new, empty file with a unique name inside a folder you choose and returns the full path to that file.
No. You must call unlink() yourself. If you want automatic deletion, use tmpfile() instead.
tempnam() returns a file name and leaves the file on disk. tmpfile() returns an open file handle and deletes the file when it is closed.
Use sys_get_temp_dir(). It returns the right folder for the current server, such as /tmp on Linux.
PHP falls back to the system temp folder and raises a notice. The function still returns a valid file name in most cases.

Next: tmpfile()

tmpfile() creates a temporary file and returns an open file handle to it. The handle is op.

tmpfile() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful