Tempnam creates a file with a unique file name

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.