如何获取临时文件路径? [英] How to get a temporary file path?

查看:99
本文介绍了如何获取临时文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用tmpfile创建一个临时文件,然后对其进行写入,然后在不再需要它时将其关闭.但是我的问题是我需要像这样的文件的绝对路径:

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:

"/var/www/html/lolo/myfile.xml"

即使使用其他功能或技巧,我还能以某种方式获得路径吗?

Can I somehow get the path, even with some other function or trick?

我希望能够从数据库下载文件,但是没有

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?

也许我可以像这样生成唯一的(uniqID)文件名,然后删除它们.还是如果有很多人正在下载,对于服务器来说这是否太耗费资源?

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?

推荐答案

以防万一有人遇到完全相同的问题.我最终做了

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

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

这篇关于如何获取临时文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆