如何获取临时文件路径?

时间:2021-08-25 16:30:59

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:

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)文件名,而不是删除它们。或者,如果有很多人下载,这对服务器来说太费劲了吗?

5 个解决方案

#1


29  

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的内容

#2


4  

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

Getting filename (or deleting file) using file handle

使用文件句柄获取文件名(或删除文件)

#3


3  

This will give you the directory. I guess after that you are on your own.

这将为您提供目录。我猜之后就是你自己了。

#4


2  

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";

#5


1  

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);

#1


29  

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的内容

#2


4  

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

Getting filename (or deleting file) using file handle

使用文件句柄获取文件名(或删除文件)

#3


3  

This will give you the directory. I guess after that you are on your own.

这将为您提供目录。我猜之后就是你自己了。

#4


2  

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";

#5


1  

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);