php PHP创建随机tmp文件并获取其完整路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16487099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP create random tmp file and get its full path
提问by Farzher
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.
我想获得已创建$temp
文件的完整路径tmpfile
。
What do I need to do to get that information?
我需要做什么才能获得这些信息?
采纳答案by Alix Axel
回答by bishop
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? PHP automatically removes the $path
when $file
goes out of scope. With tempnam
, you must manually remove the created file.
该tmpfile
方法的好处?PHP 会自动删除$path
when$file
超出范围。使用tempnam
,您必须手动删除创建的文件。