Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

"/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?

like image 995
trainoasis Avatar asked Aug 23 '12 18:08

trainoasis


People also ask

How do I find my Temp folder in Windows 10?

From the Start menu, open the Run dialog box or you can Press the "Window + R" key to open the RUN window. Type "%temp%" and click on the OK button. Note: And, you can also find the temporary files in the "Temp" folder by typing the "temp" command or by typing the "C:\Windows\Temp" path in the Run window.

How do I recover a temporary folder?

Open the Settings app and then go to Update & Security > Backup. Select Restore files from a current backup. Step 2. You can now browse to your temp file folders and you will be able to select the files that you want and then choose to restore them.


1 Answers

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

like image 192
legrandviking Avatar answered Sep 23 '22 12:09

legrandviking