257 of 410 menu

The tempnam Function

The tempnam function creates a temporary file with a unique name in the specified directory. The first parameter of the function is the path to the directory, the second is the prefix for the filename. The function returns the path to the created file or false on error.

Syntax

tempnam(directory, prefix);

Example

Let's create a temporary file in the system temporary directory with the prefix 'my':

<?php $res = tempnam(sys_get_temp_dir(), 'my'); echo $res; ?>

Code execution result (example):

'/tmp/my5d3b7f2e'

Example

Let's create a temporary file in the current directory:

<?php $res = tempnam(__DIR__, 'temp'); echo $res; ?>

Code execution result (example):

'/var/www/project/temp5d3b7f2f'

Example

Let's check the function's operation with a non-existent directory:

<?php $res = tempnam('/nonexistent', 'test'); var_dump($res); ?>

Code execution result:

false

See Also

  • the tmpfile function,
    which creates a temporary file
  • the unlink function,
    which deletes a file
  • the file_exists function,
    which checks for the existence of a file
byenru