PHP tmpfile() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

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();
CallWhat 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

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
resource|falseReturns 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.

PHP
<?php
$handle = tmpfile();

fwrite($handle, 'Hello temp');
rewind($handle);

echo fread($handle, 100), PHP_EOL;

fclose($handle);

How It Works

  1. tmpfile() creates the file and returns a handle open for reading and writing.
  2. fwrite() puts the text into the file and moves the pointer to the end.
  3. rewind() moves the pointer back to the start so we can read the data.
  4. fread() reads up to 100 bytes, and fclose() closes the file and deletes it.

Example 2 — The File Disappears After fclose()

Find the path of the temporary file and check it before and after closing.

PHP
<?php
$handle = tmpfile();
$path   = stream_get_meta_data($handle)['uri'];

echo 'Exists while open: ', file_exists($path) ? 'yes' : 'no', PHP_EOL;

fclose($handle);

echo 'Exists after fclose: ', file_exists($path) ? 'yes' : 'no', PHP_EOL;

How It Works

  1. stream_get_meta_data() returns details about the handle, including the uri path.
  2. While the handle is open, the file is on disk, so file_exists() says yes.
  3. fclose() closes the handle and PHP removes the temporary file.
  4. file_exists() now says no. This is the main difference from tempnam().

Example 3 — Process Lines from a Temporary File

Store a few lines in a temporary file, then read them back one by one.

PHP
<?php
$handle = tmpfile();

fwrite($handle, "red\ngreen\nblue\n");
rewind($handle);

$count = 0;
while (($line = fgets($handle)) !== false) {
    $count++;
    echo $count, '. ', trim($line), PHP_EOL;
}

fclose($handle);

How It Works

  1. Three color names are written to the temporary file, each on its own line.
  2. rewind() sends the pointer back to the beginning.
  3. The while loop reads one line at a time with fgets() until it returns false.
  4. trim() removes the line break and the counter numbers each line.

Frequently Asked Questions

It creates a temporary file, opens it for reading and writing, and returns the file handle. The file is deleted when it is closed.
When you call fclose() on the handle or when the script ends, whichever comes first.
Use stream_get_meta_data($handle)['uri']. It returns the full path of the temporary file.
tmpfile() gives you an open handle and cleans up for you. tempnam() gives you a file name and you must delete the file yourself.
Yes, but read the data before calling fclose(). After closing, the temporary file no longer exists.

Next: touch()

touch() sets the modified time and access time of a file. If the file does not exist, it c.

touch() 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