简体   繁体   English

PHP创建随机tmp文件并获取其完整路径

[英]PHP create random tmp file and get its full path

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. 我想获得tmpfile创建的$temp文件的完整路径。

What do I need to do to get that information? 我需要做些什么来获取这些信息?

tmpfile returns a stream-able file pointer. tmpfile返回一个可流的文件指针。

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? tmpfile方法的好处是什么? PHP automatically removes the $path when $file goes out of scope. $file超出范围时,PHP会自动删除$path With tempnam , you must manually remove the created file. 使用tempnam ,您必须手动删除创建的文件。

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

这个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM