note 98232 modified in function.tempnam by danbrown
| From: | danbrown@php.net | Date: | Thu, 03 Jun 2010 13:15:47 +0000 |
| Subject: | note 98232 modified in function.tempnam by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-169848@lists.php.net to get a copy of this message | ||
Beware: functions are not atomic. If many processes call the same function at the same time, you
may end up with unwanted behavior.
If you need your own variant of tempnam, use something like this:
<?php
function tempnam_sfx($path, $suffix)
{
do
{
$file = $path."/".mt_rand().$suffix;
$fp = @fopen($file, 'x');
}
while(!$fp);
fclose($fp);
return $file;
}
// call it like this:
$file = tempnam_sfx("/tmp", ".jpg");
?>
You may replace mt_rand() by some other random name generator, if needed.
--was--
Beware that if you write your function like Chris in his examples (tempnam with suffix or tmpdir),
you may receive unexpected results, since the functions are not atomic - if many processes call the
same function at the same time, you may end up with unwanted behavior.
If you need your own variant of tempnam, use something like this:
<?php
function tempnam_sfx($path, $suffix)
{
do
{
$file = $path."/".mt_rand().$suffix;
$fp = @fopen($file, 'x');
}
while(!$fp);
fclose($fp);
return $file;
}
// call it like this:
$file = tempnam_sfx("/tmp", ".jpg");
?>
You may replace mt_rand() by some other random name generator, if needed.
http://php.net/manual/en/function.tempnam.php