WordPress at a glance

wp_tempnam() WP 1.0

Returns a filename of a Temporary unique file. 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.

Is the basis for: download_url()
✈ 1 time = 0.000405s = fast | 50000 times = 5.08s = fast PHP 7.1.11, WP 4.9.5

No Hooks.

Return

String. a writable filename

Usage

wp_tempnam( $filename, $dir );
$filename(string)
Filename to base the Unique file off.
Default: ''
$dir(string)
Directory to store the file in.
Default: ''

Code of wp_tempnam: wp-admin/includes/file.php VER 4.9.8

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

	if ( empty( $filename ) || '.' == $filename || '/' == $filename || '\\' == $filename ) {
		$filename = time();
	}

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

Related Functions

From tag: File system (create delete files folders)

More from tag: path (directory folder)

More from category: Helper Functions

No comments
    Hello, !     Log In . Register