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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:12:38  来源:igfitidea点击:

PHP create random tmp file and get its full path

phpfilefile-iopath

提问by Farzher

After I do:

在我这样做之后:

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

I'd like to get a full path to the $tempfile that tmpfilehas created.

我想获得已创建$temp文件的完整路径tmpfile

What do I need to do to get that information?

我需要做什么才能获得这些信息?

采纳答案by Alix Axel

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

See this example.

请参阅此示例

回答by bishop

tmpfilereturns 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 tmpfileapproach? PHP automatically removes the $pathwhen $filegoes out of scope. With tempnam, you must manually remove the created file.

tmpfile方法的好处?PHP 会自动删除$pathwhen$file超出范围。使用tempnam,您必须手动删除创建的文件。