PHP create random tmp file and get its full path

倖福魔咒の 提交于 2019-12-21 06:47:01

问题


After I do:

$temp = tmpfile();
fwrite($temp, "writing to tempfile");

I'd like to get a full path to the $temp file that tmpfile has created.

What do I need to do to get that information?


回答1:


$path = tempnam(sys_get_temp_dir(), 'prefix');

See this example.




回答2:


tmpfile returns a stream-able file pointer.

To get the corresponding path, ask the stream for its meta data:

$file = tmpfile();
$path = stream_get_meta_data($file)['uri']; // eg: /tmp/phpFx0513a

The benefit of the tmpfile approach? PHP automatically removes the $path when $file goes out of scope. With tempnam, you must manually remove the created file.



来源:https://stackoverflow.com/questions/16487099/php-create-random-tmp-file-and-get-its-full-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!