get_temp_dir()WP 2.5.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

2

#1 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 );
}
0

#2 Get the path of the temporary folder

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

Changelog

Since 2.5.0 Introduced.

get_temp_dir() code WP 7.0

function get_temp_dir() {
	static $temp = '';
	if ( defined( 'WP_TEMP_DIR' ) ) {
		return trailingslashit( WP_TEMP_DIR );
	}

	if ( $temp ) {
		return trailingslashit( $temp );
	}

	if ( function_exists( 'sys_get_temp_dir' ) ) {
		$temp = sys_get_temp_dir();
		if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) {
			return trailingslashit( $temp );
		}
	}

	$temp = ini_get( 'upload_tmp_dir' );
	if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) {
		return trailingslashit( $temp );
	}

	$temp = WP_CONTENT_DIR . '/';
	if ( is_dir( $temp ) && wp_is_writable( $temp ) ) {
		return $temp;
	}

	return '/tmp/';
}