Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

tempnam

Creates a temporary file with a unique name.

Syntax

tempnam(string $directory, string $prefix): string|false

Parameters

ParameterTypeDescription
$directorystringThe folder for the file.
$prefixstringThe start of the file name.

Return value

string|falseThe path to the created file, or `false`.

Examples

A temporary file for an export:

<?php
 
$file = tempnam(sys_get_temp_dir(), 'export_');
file_put_contents($file, 'id,name');
 
echo file_get_contents($file);
 
unlink($file);

Output:

id,name

Notes

The file is created at once, so the name is guaranteed to be free. Delete it with unlink() when you are done.

See also

  • touch — Creates an empty file or updates its modification time.
  • unlink — Deletes a file.
  • uniqid — Returns an identifier based on the current time.

In the tutorial

  • Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.

Thanks for reading!

Donate to the site — we work hard for you.