PHP tempnam() Function

Definition and Usage

The tempnam() function creates a temporary file with a unique filename.

If successful, the function returns the new temporary filename. If it fails, it returns false.

Syntax

tempnam(dir,prefix)
Parameters Description
dir Required. Specifies the directory to create the temporary file in.
prefix Required. Specifies the prefix of the filename.

Description

Create a file with a unique filename in the specified directory. If the directory does not exist, tempnam() will create a file in the system's temporary directory and return its filename.

Before PHP 4.0.6, the behavior of the tempnam() function depends on the system. On Windows, the TMP environment variable will override the dir parameter, on Linux, the TMPDIR environment variable has priority, and on SVR4, the dir parameter is always used if the directory it points to exists.

Tips and Comments

Comment:If PHP cannot be specified in the dir If a file is created in the parameters, it will revert to the system default value.

Comment:The behavior of this function changed in version 4.0.3. It will also create a temporary file to avoid race conditions, that is, there may be a file with the same name in the file system between the time the string that generates the filename is produced and the script actually creates the file. Note that if the file is no longer needed, the file should be deleted, it will not be deleted automatically.

Tip:See also tmpfile()

Example

<?php
echo tempnam("C:\inetpub\testweb","TMP0");
?>

Output:

C:\inetpub\testweb\TMP1.tmp