[英]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.