Create a temp file with a specific extension using php

nikhil picture nikhil · Jan 23, 2012 · Viewed 23.4k times · Source

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.

Answer

nathan hayfield picture nathan hayfield · May 4, 2017

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');