Get the path of tmpfile in php

tmpfile is a very handy function to create temporary files. It returns the file handle/resource. If the path of the file is needed, for use with file_put_contents for example then here is a quick trick to do that

$path = array_search('uri', @array_flip(stream_get_meta_data($GLOBALS[mt_rand()]=tmpfile()))); 
file_put_contents($path, 'hello'); 
echo file_get_contents($path);

Last Updated On : 14th March 2013

Subscribe to get updates delivered to your inbox

About Silver Moon

Php developer, blogger and Linux enthusiast.
He can be reached at [email protected].
Or find him on

Leave a comment