How do I create a temporary file with a specified extension in php. I came across tempnam()
but using it the extension can't be specified.
Definition and Usage. The tmpfile() function creates a temporary file with a unique name in read-write (w+) mode. Note: The file is automatically removed when closed, with fclose() or when the script ends.
To create and use a temporary fileThe application opens the user-provided source text file by using CreateFile. The application retrieves a temporary file path and file name by using the GetTempPath and GetTempFileName functions, and then uses CreateFile to create the temporary file.
Temporary files typically have a . TMP or . TEMP file extension, but any naming convention might be used.
php stores all temporary files, that includes uploaded files, in the temporary files directory as specified in the php. ini. Note that for uploads, those files might be removed as soon as the script the file was uploaded to was terminated (so unless you delay that script, you probably won't see the uploaded file).
Easiest way i have found is to create tempfile and then just rename it. For example:
$tmpfname = tempnam(sys_get_temp_dir(), "Pre_"); rename($tmpfname, $tmpfname .= '.pdf');
my way is using tempnam
$file = tempnam(sys_get_temp_dir(), 'prefix'); file_put_contents($file.'.extension', $data); { //use your file } unlink($file);//to delete an empty file that tempnam creates unlink($file.'.extension');//to delete your file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With