The tmpfile() function in PHP is an inbuilt function which is used to create a temporary file with a unique name in read-write (w+) mode.
The file created using tmpfile() function gets automatically deleted when close using fclose() or when there are no remaining references to the file handle.
The end of the script also results in the removal of the temporary file created using tmpfile() function.
The tmpfile() function takes no parameters and it returns a file handle which is similar to the one returned by fopen(), for the new file or FALSE on failure.
It returns a file handle for the new file on success or FALSE on failure.
Errors And Exception:
- The temporary file is automatically removed when it is closed with fclose(), or when the script ends.
- The tmpfile() function returns Boolean False but many times it happens that it returns a non-Boolean value which evaluates to False.
Input : $temp_pointer = tmpfile(); fwrite($temp_pointer, 'temporary data'); fclose(temp_pointer); Output : 1 Input : $temp_pointer = tmpfile(); fwrite($temp_pointer, "GeeksforGeeks"); echo fread($temp_pointer, 2048); fclose($temp); Output : Geeksforgeeks
Below programs illustrate the tmpfile() function.