get_temp_dir()WP-CLI 1.0

Gets the path to the folder where temporary files can be written. With a slash at the end.

The temporary files folder is selected in the following order (the first folder for which there is write permission is returned):

  1. Constant WP_TEMP_DIR. If this constant defines the path to the temporary folder, it will be used. By default, this constant is not defined in WP. The constant should be defined in the wp-config.php file.
  2. PHP function sys_get_temp_dir() - returns the path to the temporary folder on the server.
  3. PHP option ini_get('upload_tmp_dir') - contains the path to the temporary folder on the server.
  4. Constant WP_CONTENT_DIR - contains the path to the WP content folder.
  5. /tmp/ - hardcoded path to the folder on the server.

Each folder is first checked for the ability to write a file to it, through wp_is_writable().

To create a temporary file in the temporary folder, use wp_tempnam()

1 time — 0.00001 sec (speed of light) | 50000 times — 0.03 sec (speed of light) | PHP 7.1.11, WP 4.9.5

No Hooks.

Returns

String. The path to the writable temporary folder on the server.

Usage

$temp_dir = get_temp_dir();

Examples

0

#1 Get the path of the temporary folder

echo get_temp_dir(); // /server/tmp/
0

#2 Create directory my_test in the temporary directory, if it does not already exist

$my_tmp_dir = get_temp_dir() . '/my_test';
if( ! is_dir($my_tmp_dir) ){
	mkdir( $my_tmp_dir );
}

get_temp_dir() code WP-CLI 2.8.0-alpha

function get_temp_dir() {
	static $temp = '';

	if ( $temp ) {
		return $temp;
	}

	// `sys_get_temp_dir()` introduced PHP 5.2.1. Will always return something.
	$temp = trailingslashit( sys_get_temp_dir() );

	if ( ! is_writable( $temp ) ) {
		WP_CLI::warning( "Temp directory isn't writable: {$temp}" );
	}

	return $temp;
}