wp_tempnam()
Creates an empty temporary writable file with a unique name in the temporary directory. After use, the file must be deleted using unlink();
The temporary directory can be specified or it will be obtained using get_temp_dir().
After using the function, the created file must be deleted using unlink().
The file name can be specified manually in the first parameter $filename, which will be used as a base for creating a truly unique file name.
To ensure uniqueness, a hash and the extension .tmp are added to the provided file name. For example, if you specify myfile, you will get: /tmp/myfile-ADfs54f.tmp. If no base file name is specified, the current time stamp will be used as the base name: time().
To use the function on the front end, you need to include the file:
require_once ABSPATH . 'wp-admin/includes/file.php';
No Hooks.
Returns
String. The path to the created file. The file is physically created and must be deleted.
Usage
wp_tempnam( $filename, $dir );
- $filename(string)
- The file name that will be used as a base for creating the file name.
Default: '' - $dir(string)
- The path to the temporary directory where the file name will be generated. If not specified, the directory will be obtained through the function get_temp_dir().
Default: ''
Examples
#1 Example of what the function outputs
require_once ABSPATH . 'wp-admin/includes/file.php'; $filename = wp_tempnam(); //> /tmp/1524879129-OkwJkv.tmp // modify the file and do something with it unlink( $filename ); // delete the file
More examples:
$filename = wp_tempnam( 'my_log_file' ); //> /tmp/my_log_file-BmwSQw.tmp unlink( $filename ); $filename = wp_tempnam( 'my_log_file.log' ); //> /tmp/my_log_file-AreTYU.tmp unlink( $filename ); $filename = wp_tempnam( '', $_SERVER['DOCUMENT_ROOT'].'/' ); //> /home/example.com/public_html/1524879213-ieQsRX.tmp unlink( $filename );
Changelog
| Since 2.6.0 | Introduced. |