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

Returns a filename of a Temporary unique file.


Description Description

Please note that the calling function must unlink() this itself.

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.


Top ↑

Parameters Parameters

$filename

(string) (Optional) Filename to base the Unique file off.

Default value: ''

$dir

(string) (Optional) Directory to store the file in.

Default value: ''


Top ↑

Return Return

(string) a writable filename


Top ↑

Source Source

File: wp-admin/includes/file.php

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

	if ( empty( $filename ) || '.' == $filename || '/' == $filename || '\\' == $filename ) {
		$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  = $dir . wp_unique_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;
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Nabil

    Keep in mind that (1) the function doens’t delete the file and that (2) you can’t use extensions, but you can use my function which supports extensions and deletes files at the end of the script execution:

    Examples:

    // Basic
    $filename = 'hello.txt';
    $content = 'Hello World!';
    $file = wporg_k_tempnam( $file, $content );
    
    // Better - salt in the file name 
    $filename = 'hello-' . wp_generate_password( 5, false ) . '.txt';
    $content = 'Hello World!';
    $file = wporg_k_tempnam( $file, $content );
    

    The function:

    /**
     * Create temporary file in system temporary directory.
     *
     * @author Nabil Kadimi - https://kadimi.com
     *
     * @param  string $name    File name.
     * @param  string $content File contents.
     * @return string File path.
     */
    function wporg_k_tempnam( $name, $content ) {
    	$sep = DIRECTORY_SEPARATOR;
    	$file = $sep . trim( sys_get_temp_dir(), $sep ) . $sep . ltrim( $name, $sep );
    	file_put_contents( $file, $content );
    	register_shutdown_function( function() use( $file ) {
    		@unlink( $file );
    	} );
    	return $file;
    }

You must log in before being able to contribute a note or feedback.