Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Files and I/O

PHP tempnam(), tmpfile() and sys_get_temp_dir()

Reference for PHP temporary files: sys_get_temp_dir(), tempnam() and tmpfile(). How they differ, permissions, automatic cleanup and safe usage patterns.

PHP has three building blocks for temporary files. sys_get_temp_dir() returns the directory the system uses for scratch files. tempnam() creates a new, empty, uniquely named file in a directory you choose and returns its path. tmpfile() creates an anonymous file, opens it for reading and writing, and returns a handle; PHP deletes it when you close the handle or the script ends.

Use tmpfile() when you only need scratch space during one script. Use tempnam() when another function or program needs a real file name, and remember that you must delete that file yourself.

PHP
<?php
$path = tempnam(sys_get_temp_dir(), 'job_');
file_put_contents($path, 'data');
unlink($path);

Try it

PHP tempnam() creates the file for you

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP tmpfile() as scratch space

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
sys_get_temp_dir(): string
tempnam(string $directory, string $prefix): string|false
tmpfile(): resource|false
Name Description
$directory Folder in which tempnam() creates the file. If it does not exist, PHP falls back to the system temp directory (and may emit a notice).
$prefix Start of the generated file name. Only the first few characters are guaranteed to be kept, so keep it short.

Return value:

  • sys_get_temp_dir() returns the temp directory path without a trailing slash.
  • tempnam() returns the path of the new, empty file, or false if it could not be created.
  • tmpfile() returns a stream resource (opened for reading and writing in binary mode), or false on failure.

Errors / Exceptions: none are thrown. Check for false.

Usage notes

  • tempnam() really creates the file. It reserves a unique name atomically, which avoids the race you get from inventing a name with uniqid() and then opening it. The file is empty and, on Unix, has mode 0600.
  • It never deletes the file for you. Call unlink() in a finally block, or use register_shutdown_function(), so crashes do not leave files behind.
  • tmpfile() cleans up after itself, on fclose() and at script end. You rarely need its name, and that is the point: other programs are unlikely to open it by accident.
  • Use a subfolder you control for long-lived data. The system temp directory is shared with other programs and may be cleaned at reboot or by a timer.
  • Do not trust the name for secrets. The name is unique but not meant to be unguessable. Do not treat it as an access token.
  • Extensions: tempnam() cannot add a suffix such as .csv. If a library insists on one, rename the file and delete the renamed file later.
  • In-memory alternative: php://memory and php://temp avoid the disk, see php:// streams.
  • php://temp spills to disk automatically after a size threshold (2 MB by default), so it suits data of unknown size.