get_temp_dir()WP-CLI 1.0

Determine a writable directory for temporary files.

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.

Return

String.

Usage

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