Create a temporary file with a unique name in read-write mode (w+
).
<?php $temp = tmpfile(); fwrite($temp, “Testing, testing.”); //Rewind to the start of file rewind($temp); //Read 1k from file echo fread($temp,1024); //This removes the file fclose($temp); ?> |
The result of the code above will be:
Testing, testing. |
The tmpfile() function creates a temporary file with a unique name in read-write (w+) mode.
Note: The file is automatically deleted when closed using fclose() or when the script terminates.
Tip: Also consider using the tempnam() function.
tmpfile() |
Return Value: |
Returns a file handle (similar to the one returned by fopen()) for the new file; FALSE on failure. |
PHP Version: |
4.0+ |