get_temp_dir

Determine a writable directory for temporary files.

Description

(string) get_temp_dir(); 

Function's preference is the return value of sys_get_temp_dir(), followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR, before finally defaulting to /tmp/

In the event that this function does not find a writable location, It may be overridden by the WP_TEMP_DIR constant in your wp-config.php file.

Returns (string)

Writable temporary directory.


Usage

  1. if ( !function_exists( 'get_temp_dir' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = get_temp_dir(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function get_temp_dir() { 
  2. static $temp = ''; 
  3. if ( defined('WP_TEMP_DIR') ) 
  4. return trailingslashit(WP_TEMP_DIR); 
  5.  
  6. if ( $temp ) 
  7. return trailingslashit( $temp ); 
  8.  
  9. if ( function_exists('sys_get_temp_dir') ) { 
  10. $temp = sys_get_temp_dir(); 
  11. if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) 
  12. return trailingslashit( $temp ); 
  13.  
  14. $temp = ini_get('upload_tmp_dir'); 
  15. if ( @is_dir( $temp ) && wp_is_writable( $temp ) ) 
  16. return trailingslashit( $temp ); 
  17.  
  18. $temp = WP_CONTENT_DIR . '/'; 
  19. if ( is_dir( $temp ) && wp_is_writable( $temp ) ) 
  20. return $temp; 
  21.  
  22. return '/tmp/';