wppaste
WordPress

wp_tempnam( string $filename = '', string $dir = '' ): string

Since
2.6.0
Source
wp-admin/includes/file.php:671
Returns a filename of a temporary unique file.

Description

Please note that the calling function must delete or move the file.

The filename is based off the passed parameter or defaults to the current unix timestamp, while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.

Compatibility

WordPress
since 2.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$filenamestringoptional
Filename to base the Unique file off. Default empty.Default: ''
$dirstringoptional
Directory to store the file in. Default empty.Default: ''

Return value

string
A writable filename.

Performance profile

How much work a call to wp_tempnam() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via fopen().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
22–61

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 81.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
8

8 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • filesystemfilesystem accessfopen()called directly
  • hookthird-party callbacksapply_filters()one call below wp_tempnam()

Further down the call graph this can also reach transient, cache, option, serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 32 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_tempnam() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!empty($dir) && !empty($filename) && !$filename22basename(), wp_tempnam()
!empty($dir)23–25uniqid(), basename(), wp_tempnam()
empty($dir) && !empty($filename) && !$filename25get_temp_dir(), basename(), wp_tempnam()
empty($dir)26–28get_temp_dir(), uniqid(), basename(), wp_tempnam()
!empty($dir) && !empty($filename) && !$filename && $characters_over_limit41basename(), wp_generate_password(), wp_unique_filename(), wp_tempnam()
!empty($dir) && $characters_over_limit42–44uniqid(), basename(), wp_generate_password(), wp_unique_filename(), wp_tempnam()
empty($dir) && !empty($filename) && !$filename && $characters_over_limit44get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), wp_tempnam()
empty($dir) && $characters_over_limit45–47get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), wp_tempnam()
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit46basename(), wp_generate_password(), wp_unique_filename(), fopen(), fclose()
!empty($dir) && !$characters_over_limit47–49uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), fclose()
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && !is_writable()48basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable()
!empty($dir) && !$characters_over_limit && !is_writable()49–51uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable()
20 further outcomes, up to 61 instructions
empty($dir) && !empty($filename) && !$filename && !$characters_over_limit49get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), fclose()
empty($dir) && !$characters_over_limit50–52get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), fclose()
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && !is_writable()51basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), fclose()
empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && !is_writable()51get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable()
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && is_writable() && !file_exists()52basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists()
!empty($dir) && !$characters_over_limit && !is_writable()52–54uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), fclose()
empty($dir) && !$characters_over_limit && !is_writable()52–54get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable()
!empty($dir) && !$characters_over_limit && is_writable() && !file_exists()53–55uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists()
empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && !is_writable()54get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), fclose()
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && is_writable() && !file_exists()55basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), fclose()
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && is_writable() && file_exists()55basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), wp_tempnam()
empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && is_writable() && !file_exists()55get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists()
empty($dir) && !$characters_over_limit && !is_writable()55–57get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), fclose()
!empty($dir) && !$characters_over_limit && is_writable() && !file_exists()56–58uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), fclose()
!empty($dir) && !$characters_over_limit && is_writable() && file_exists()56–58uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), wp_tempnam()
empty($dir) && !$characters_over_limit && is_writable() && !file_exists()56–58get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists()
empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && is_writable() && !file_exists()58get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), fclose()
empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && is_writable() && file_exists()58get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), wp_tempnam()
empty($dir) && !$characters_over_limit && is_writable() && !file_exists()59–61get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), fclose()
empty($dir) && !$characters_over_limit && is_writable() && file_exists()59–61get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), wp_tempnam()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8122–619
8.58122–619
8.48122–6198 fewer instructions than PHP 8.3
8.38927–649
8.28927–649
8.18927–649
7.48927–649

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 4

Used by · 8

Source code

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;}

Changelog

Introduced in 2.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-admin/includes/file.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.