PHP tempnam() Function

Definition and Usage

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

If successful, this 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 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 takes precedence, and on SVR4 the dir parameter is always used if the directory it points to exists.

Hints and Comments

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

Comment:The behavior of this function has changed in version 4.0.3. It will also create a temporary file to avoid competition, that is, there may be a situation where a file with the same name as the string generated as a filename may exist in the file system between the time the script actually creates the file and the file is created. Note that if the file is no longer needed, it should be deleted; it will not be deleted automatically.

Hint:See also tmpfile()

Example

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

Output:

C:\inetpub\testweb\TMP1.tmp