70

Quickie...

Is there a way to retrieve the path of a file created by tmpfile()?

Or do I need to do it myself with tempnam()?

2 Answers 2

123

It seems stream_get_meta_data() also works :

$tmpHandle = tmpfile();
$metaDatas = stream_get_meta_data($tmpHandle);
$tmpFilename = $metaDatas['uri'];
fclose($tmpHandle);
7
  • 23
    +1 For actually answering the question. As of PHP 5.3 you can simply use stream_get_meta_data($tmpHandle)['uri'].
    – flu
    Aug 13, 2013 at 12:02
  • $foo()['bar'] not work on my PHP 5.3.13, but otherwise this answer is correct answer on question asked above.
    – DarkSide
    Oct 4, 2013 at 12:38
  • Great! sys_get_temp_dir() not available on old php 5.1 server :(
    – Pete
    Jan 15, 2014 at 23:15
  • 5
    Of course you cannot access this file any more after fclose() call, it deletes the temporary file.
    – Oytun
    Sep 30, 2015 at 16:22
  • searched for this for about an hour..! Apr 19, 2017 at 4:51
-1

Like this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.