note 34303 added to function.tempnam
| From: | php-general at lists dot php dot net | Date: | Wed, 23 Jul 2003 15:56:10 +0000 |
| Subject: | note 34303 added to function.tempnam | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-52496@lists.php.net to get a copy of this message | ||
The tempnam() function will not let you specify a postfix to the filename created. Here is a
function that will create a new filename with pre and post fix'es. Not returns false if it
can't create in the dir specified where tempnam() creates in the systems temp dir.
function newtempnam($dir, $prefix, $postfix){
/* Creates a new non-existant file with the specified post and pre fixes */
if ($dir[strlen($dir) - 1] == '/') {
$trailing_slash = "";
} else {
$trailing_slash = "/";
}
/*The PHP function is_dir returns true on files that have no extension.
The filetype function will tell you correctly what the file is */
if (!is_dir(realpath($dir)) || filetype(realpath($dir)) != "dir") {
// The specified dir is not actualy a dir
return false;
}
if (!is_writable($dir)){
// The directory will not let us create a file there
return false;
}
do{ $seed = substr(md5(microtime().posix_getpid()), 0, 8);
$filename = $dir . $trailing_slash . $prefix . $seed . $postfix;
} while (file_exists($filename));
$fp = fopen($filename, "w");
fclose($fp);
return $filename;
}
----
Manual Page -- http://www.php.net/manual/en/function.tempnam.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34303
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34303&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34303&report=yes