wp_tempnam()WP 2.6.0

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';
Used By: download_url()
1 time — 0.000405 sec (fast) | 50000 times — 5.08 sec (fast) | PHP 7.1.11, WP 4.9.5

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

0

#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.

wp_tempnam() code WP 7.0

function wp_tempnam( $filename = '', $dir = '' ) {
	if ( empty( $dir ) ) {
		$dir = get_temp_dir();
	}

	if ( empty( $filename ) || in_array( $filename, array( '.', '/', '\\' ), true ) ) {
		$filename = uniqid();
	}

	// Use the basename of the given file without the extension as the name for the temporary directory.
	$temp_filename = basename( $filename );
	$temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename );

	// If the folder is falsey, use its parent directory name instead.
	if ( ! $temp_filename ) {
		return wp_tempnam( dirname( $filename ), $dir );
	}

	// Suffix some random data to avoid filename conflicts.
	$temp_filename .= '-' . wp_generate_password( 6, false );
	$temp_filename .= '.tmp';
	$temp_filename  = wp_unique_filename( $dir, $temp_filename );

	/*
	 * Filesystems typically have a limit of 255 characters for a filename.
	 *
	 * If the generated unique filename exceeds this, truncate the initial
	 * filename and try again.
	 *
	 * As it's possible that the truncated filename may exist, producing a
	 * suffix of "-1" or "-10" which could exceed the limit again, truncate
	 * it to 252 instead.
	 */
	$characters_over_limit = strlen( $temp_filename ) - 252;
	if ( $characters_over_limit > 0 ) {
		$filename = substr( $filename, 0, -$characters_over_limit );
		return wp_tempnam( $filename, $dir );
	}

	$temp_filename = $dir . $temp_filename;

	$fp = @fopen( $temp_filename, 'x' );

	if ( ! $fp && is_writable( $dir ) && file_exists( $temp_filename ) ) {
		return wp_tempnam( $filename, $dir );
	}

	if ( $fp ) {
		fclose( $fp );
	}

	return $temp_filename;
}