Creating a temporary file

PHP:
Creating a temporary file

How to:

PHP helps you create temporary files with the tmpfile() function, which creates a file for you in your system’s temp directory. Here’s a quick example:

<?php
$tempFile = tmpfile();
fwrite($tempFile, "Hello, temporary world!");
rewind($tempFile);

echo fread($tempFile, 1024); // Read what we wrote to the file

fclose($tempFile); // The temporary file is removed automatically
?>

Sample Output:

Hello, temporary world!

You can also use tempnam() to get a file name that you can manage yourself:

<?php
$tempFilePath = tempnam(sys_get_temp_dir(), 'Tux');
file_put_contents($tempFilePath, "Penguins are cool!");

echo file_get_contents($tempFilePath); // Read the content

unlink($tempFilePath); // Delete the file when you're done
?>

Sample Output:

Penguins are cool!

Deep Dive

The tmpfile() function has been in PHP since the early days. It handles file creation and cleanup for you, nicely sidestepping potential security risks of leaving sensitive data hanging around.

On the flip side, tempnam() gives you just a name, leaving the file management in your hands. One caveat: always remember to unlink() the file when you’re done.

These temporary files are usually stored in your system’s default temp directory, which you can find with sys_get_temp_dir(). This location can vary based on your operating system and environment configuration.

You also have alternatives like tempnam() and tmpfile(), and there’s the fancier sys_get_temp_dir() for getting that elusive temp directory. But remember the golden rule with temporary files: tidy up after yourself—PHP does some of this automatically, but it’s good practice to be explicit.

See Also