String tempnam (string $ Dir, string $ prefix)
Create a file with a unique file name in the specified directory. If the directory does not exist, tempnam () generates a file in the temporary directory of the system and returns the file name.
Before PHP 4.0.6, the behavior of the tempnam () function depends on the system. In Windows, the TMP environment variable will go beyond the Dir parameter. in Linux, The tmpdir environment variable takes precedence. In svr4, the Dir parameter is always used if it points to a directory. If you have any questions, refer to the tempnam (3) function in the system documentation.
Note: If php cannot create a file in the specified dir parameter, it is returned to the default value of the system.
A new temporary file name is returned. If an error occurs, false is returned.
Example #1 tempnam () Example
<? PHP
$ Tmpfname = tempnam ("/tmp", "foo ");
$ Handle = fopen ($ tmpfname, "W ");
Fwrite ($ handle, "writing to tempfile ");
Fclose ($ handle );
// Do here something
Unlink ($ tmpfname );
?>
However, this function is not perfect. If multiple processes use this function at the same time, unexpected results may occur. If you need to create your own files, you can use the followingCode:
<? 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 can use other Rand functions to replace the mt_rand () method.