Hudzilla.org - the homepage of Paul Hudson
Contents > Files Wish List | Report Bug | About Me ]

8.4     Temporary files: tmpfile()

This is NOT the latest copy of this book; click here for the latest version.

resource tmpfile ( void )

Very often you will find you want to work with a file as if it were a "scratchpad" - a holding area where you can write out temporary data for later use. To make this as easy as possible, PHP has a function called tmpfile() which takes no parameters, but will create a temporary file on the system, fopen() it for you, and send back the file handle as its return value.

That file is then yours to read from and write to all you wish, and is deleted as soon as you fclose() the file handle or the script ends. Here is an example of tmpfile() in use:

<?php
    $handle
= tmpfile();
    
$numbytes = fwrite($handle, $mystring);
    
fclose($handle);
    print
"$numbytes bytes written\n";
?>

As you can see, tmpfile() is a drop-in replacement for fopen() ing a known file, so it is easy to make use of.





<< 8.3.3 Deleting files with unlink()   8.5 Other file functions: rewind(), and fseek() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Guna - 07 Sep 2008

In which folder the temporary file is created?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow