wp_tempnam( string $filename = '', string $dir = '' ): string
- Since
- 2.6.0
- Source
wp-admin/includes/file.php:671
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
- Scaling
- Constant
- Instructions
- 22–61
- Plugin surface
- None
- Called by
- 8
Reads or writes the filesystem via fopen().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 81.
Nothing here hands control to plugin code.
8 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
fopen()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($dir) && !empty($filename) && !$filename | 22 | basename(), wp_tempnam() |
!empty($dir) | 23–25 | uniqid(), basename(), wp_tempnam() |
empty($dir) && !empty($filename) && !$filename | 25 | get_temp_dir(), basename(), wp_tempnam() |
empty($dir) | 26–28 | get_temp_dir(), uniqid(), basename(), wp_tempnam() |
!empty($dir) && !empty($filename) && !$filename && $characters_over_limit | 41 | basename(), wp_generate_password(), wp_unique_filename(), wp_tempnam() |
!empty($dir) && $characters_over_limit | 42–44 | uniqid(), basename(), wp_generate_password(), wp_unique_filename(), wp_tempnam() |
empty($dir) && !empty($filename) && !$filename && $characters_over_limit | 44 | get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), wp_tempnam() |
empty($dir) && $characters_over_limit | 45–47 | get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), wp_tempnam() |
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit | 46 | basename(), wp_generate_password(), wp_unique_filename(), fopen(), fclose() |
!empty($dir) && !$characters_over_limit | 47–49 | uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), fclose() |
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && !is_writable() | 48 | basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable() |
!empty($dir) && !$characters_over_limit && !is_writable() | 49–51 | uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable() |
20 further outcomes, up to 61 instructions
empty($dir) && !empty($filename) && !$filename && !$characters_over_limit | 49 | get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), fclose() |
empty($dir) && !$characters_over_limit | 50–52 | get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), fclose() |
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && !is_writable() | 51 | basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), fclose() |
empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && !is_writable() | 51 | get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable() |
!empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && is_writable() && !file_exists() | 52 | basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists() |
!empty($dir) && !$characters_over_limit && !is_writable() | 52–54 | uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), fclose() |
empty($dir) && !$characters_over_limit && !is_writable() | 52–54 | get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable() |
!empty($dir) && !$characters_over_limit && is_writable() && !file_exists() | 53–55 | uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists() |
empty($dir) && !empty($filename) && !$filename && !$characters_over_limit && !is_writable() | 54 | get_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() | 55 | 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() | 55 | basename(), 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() | 55 | get_temp_dir(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists() |
empty($dir) && !$characters_over_limit && !is_writable() | 55–57 | get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), fclose() |
!empty($dir) && !$characters_over_limit && is_writable() && !file_exists() | 56–58 | uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), fclose() |
!empty($dir) && !$characters_over_limit && is_writable() && file_exists() | 56–58 | uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), wp_tempnam() |
empty($dir) && !$characters_over_limit && is_writable() && !file_exists() | 56–58 | get_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() | 58 | get_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() | 58 | get_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–61 | get_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–61 | get_temp_dir(), uniqid(), basename(), wp_generate_password(), wp_unique_filename(), fopen(), is_writable(), file_exists(), wp_tempnam() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 81 | 22–61 | 9 | |
| 8.5 | 81 | 22–61 | 9 | |
| 8.4 | 81 | 22–61 | 9 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 89 | 27–64 | 9 | |
| 8.2 | 89 | 27–64 | 9 | |
| 8.1 | 89 | 27–64 | 9 | |
| 7.4 | 89 | 27–64 | 9 |
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
- get_temp_dir()Determines a writable directory for temporary files.
- wp_tempnam()Returns a filename of a temporary unique file.
- wp_generate_password()Generates a random password drawn from the defined set of characters.
- wp_unique_filename()Gets a filename that is sanitized and unique for the given directory.
Used by · 8
- WP_Customize_Manager::import_theme_starter_content()Imports theme starter content into the customized state.
- WP_Filesystem_FTPext::get_contents()Reads entire file into a string.
- WP_Filesystem_FTPext::put_contents()Writes a string to a file.
- WP_Filesystem_ftpsockets::get_contents()Reads entire file into a string.
- WP_Filesystem_ftpsockets::put_contents()Writes a string to a file.
- WP_REST_Attachments_Controller::upload_from_data()Handles an upload via raw POST data.
- download_url()Downloads a URL to a local temporary file using the WordPress HTTP API.
- wp_tempnam()Returns a filename of a temporary unique file.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.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.