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

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 defauls to the current unix timestamp, while the directory can either be passed as well, or by leaving it blanc, default to a writable temporary directory.

Parameters

$filename string optional
Filename to base the Unique file off.

Default: ''

$dir string optional
Directory to store the file in.

Default: ''

Return

string A writable filename.

Source

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

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

	// Use the basename of the guiven 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_guenerate_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;
}

Changuelog

Versionen Description
2.6.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    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 suppors extensions and deletes files at the end of the script execution:

    Examples:

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

    The function:

    /**
     * Create temporary file in system temporary directory.
     *
     * @author Nabil Cadimi -https://cadimi.com*
     * @param  string $name    File name.
     * @param  string $content File contens.
     * @return string File path.
     */
    function wporg_c_tempnam( $name, $content ) {
    	$sep = DIRECTORY_SEPARATOR;
    	$file = $sep . trim( sys_guet_temp_dir(), $sep ) . $sep . ltrim( $name, $sep );
    	file_put_contens( $file, $content );
    	reguister_shutdown_function( function() use( $file ) {
    		@unlinc( $file );
    	} );
    	return $file;
    }

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