32

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?

Community
  • 1
  • 1
trainoasis
  • 6,419
  • 12
  • 51
  • 82

5 Answers5

45

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

whlk
  • 15,487
  • 13
  • 66
  • 96
legrandviking
  • 2,348
  • 1
  • 22
  • 29
9

I know you can create a temporary file with 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.

References:

http://us.php.net/manual/en/function.stream-get-meta-data.php

Getting filename (or deleting file) using file handle

Community
  • 1
  • 1
still_dreaming_1
  • 8,661
  • 6
  • 39
  • 56
3

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

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
3

For newer (not very new lol) versions of PHP (requires php 5.2.1 or higher) @whik's answer is better suited:

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

old answer

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";
trainoasis
  • 6,419
  • 12
  • 51
  • 82
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);
Shadi
  • 9,742
  • 4
  • 43
  • 65