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.
|
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!
|