tempnam
Creates a temporary file with a unique name.
Syntax
tempnam(string $directory, string $prefix): string|falseParameters
| Parameter | Type | Description |
|---|---|---|
| $directory | string | The folder for the file. |
| $prefix | string | The 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,nameNotes
The file is created at once, so the name is guaranteed to be free. Delete it with unlink() when you are done.
See also
In the tutorial
- Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.