PHP | tmpfile() Function

PHP tmpfile() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP tmpfile() Function.

The built-in PHP tmpfile() function is used for creating a temporary file with a unique name in the read-write (w+) mode.

Syntax

Following below is the syntax to use this function -

resource tmpfile ( void )


This function creates a temporary file with a unique name in the read-write (w+) mode and then returns a filehandle. The file is automatically removed when it is closed (for example by calling the fclose() function or when there are no remaining references to the filehandle returned by tmpfile()), or when the script ends.

Return Value

This function returns the filehandle similar to the one returned by the fopen() function for the new file, or false on failure.

Example

Try out the below example -

<?php
   $temp = tmpfile();
   fwrite($temp, "Webdesigntutorialz!!!!");
   rewind($temp);  // Rewind to start of a file
   echo fread($temp, 1024);  // Read 1k from a file
   
   fclose($temp);  // it removes the file
?>

Output

When the above code is executed, it will produce the following result -

Webdesigntutorialz!!!!


Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP touch() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.