What is the tmpfile() function in PHP?

Overview

The tmpfile function in PHP is used to create a file with a name. We can also read and write to that file. The general syntax for the tmpfile function is as follows:

tmpfile() 

Parameter

The tmpfile function has no compulsory parameters.

Return value

The tmpfile function returns the handle for the temporary file made if it successfully creates the file. Otherwise, the function returns FALSE.

Example

The following example will help you understand the tmpfile function better. First, we use the tmpfile function to create a file and then write Hello World to the file. To check whether this has been written to the file, we rewind the pointer to the beginning of the temporary file and read from it. As it reads the text perfectly from the file, this means that the file was successfully created and written to.

<?php
$file = tmpfile();
fwrite($file, "Hello World!");
rewind($file);
echo fread($file,1000);
fclose($file);
?>

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved