Photo of Danny Berger
Path-based tmpfile in PHP
Post |

PHP has the tmpfile function for creating a file handle which will automatically be destroyed when it is closed or when the script ends. PHP also has the tempnam function which takes care of creating the file and returning the path, but doesn't automatically destroy the file.

To get the best of both worlds (temp file + auto-destroy), I have found this useful:


function tmpfilepath() {
    $path = stream_get_meta_data(tmpfile())['uri'];

        function () use ($path) {

    return $path;