trainoasis trainoasis - 1 year ago 52
PHP Question

How to get a temporary file path?

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:


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

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?


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



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