php 如何获取临时文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12097631/
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
How to get a temporary file path?
提问by trainoasis
I know you can create a temporary file with tmpfile and than write to it, and close it when it is not needed anymore. But the problem I have is that I need the absolute path to the file like this:
我知道您可以使用 tmpfile 创建一个临时文件,然后写入它,并在不再需要时将其关闭。但我遇到的问题是我需要这样的文件的绝对路径:
"/var/www/html/lolo/myfile.xml"
Can I somehow get the path, even with some other function or trick?
即使使用其他功能或技巧,我能否以某种方式获得路径?
EDIT:
编辑:
I want to be able to download the file from the database, but without
我希望能够从数据库下载文件,但没有
$fh = fopen("/var/www/html/myfile.xml", 'w') or die("no no");
fwrite($fh, $fileData);
fclose($fh);
because if I do it like this, there is a chance of overlapping, if more people try to download the same file at exactly the same time. Or am I wrong?
因为如果我这样做,就有可能重叠,如果更多的人试图在完全相同的时间下载同一个文件。还是我错了?
EDIT2:
编辑2:
Maybe I can just generate unique(uniqID) filenames like that, and than delete them. Or can this be too consuming for the server if many people are downloading?
也许我可以像这样生成唯一的(uniqID)文件名,而不是删除它们。或者,如果很多人都在下载,这对服务器来说是否太消耗了?
采纳答案by trainoasis
Just in case someone encounters exactly the same problem. I ended up doing
以防万一有人遇到完全相同的问题。我最终做了
$fh = fopen($filepath, 'w') or die("Can't open file $name for writing temporary stuff.");
fwrite($fh, $fileData);
fclose($fh);
and
和
unlink($filepath);
at the end when file is not needed anymore.
最后当不再需要文件时。
Before that, I generated filename like that:
在此之前,我生成了这样的文件名:
$r = rand();
$filepath = "/var/www/html/someDirectory/$name.$r.xml";
回答by legrandviking
There are many ways you can achieve this, here is one
有很多方法可以实现这一点,这里是一种
<?php
// Create a temp file in the temporary
// files directory using sys_get_temp_dir()
$temp_file = tempnam(sys_get_temp_dir(), 'MyFileName');
echo $temp_file;
?>
The above example will output something similar to: /var/tmp/MyFileNameX322.tmp
上面的例子将输出类似于:/var/tmp/MyFileNameX322.tmp
回答by still_dreaming_1
I know you can create a temporary file with tmpfile
我知道你可以用 tmpfile 创建一个临时文件
That is a good start, something like this will do:
这是一个好的开始,像这样的事情会做:
$fileHandleResource = tmpfile();
Can I somehow get the path, even with some other function or trick?
即使使用其他功能或技巧,我能否以某种方式获得路径?
Yes:
是的:
$metaData = stream_get_meta_data($fileHandleResource);
$filepath = $metaData['uri'];
This approach has the benefit of leaving it up to PHP to pick a good place and name for this temporary file, which could end up being a good thing or a bad thing depending on your needs. But it is the simplest way to do this if you don't yet have a specific reason to pick your own directory and filename.
这种方法的好处是让 PHP 为这个临时文件选择一个好的位置和名称,这最终可能是好事还是坏事,具体取决于您的需要。但是,如果您还没有特定的理由来选择自己的目录和文件名,那么这是最简单的方法。
References:
参考:
http://us.php.net/manual/en/function.stream-get-meta-data.php
http://us.php.net/manual/en/function.stream-get-meta-data.php
回答by Ed Heal
回答by shadi
I just generated a temporary file, deleted it, and created a folder with the same name
我刚刚生成了一个临时文件,删除了它,并创建了一个同名的文件夹
$tempFolder = tempnam(sys_get_temp_dir(), 'MyFileName');
unlink($tempFolder);
mkdir($tempFolder);

