recurse_dirsize

Get the size of a directory recursively.

Description

(int|false) recurse_dirsize( (string) $directory, (null) $exclude = null ); 

Used by get_dirsize() to get a directory's size when it contains other directories.

Returns (int|false)

Size in MB if a valid directory. False if not.

Parameters (2)

0. $directory (string)
Full path of a directory.
1. $exclude — Optional. (null) => null
Full path of a subdirectory to exclude from the total.

Usage

  1. if ( !function_exists( 'recurse_dirsize' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-functions.php'; 
  3.  
  4. // Full path of a directory. 
  5. $directory = ''; 
  6.  
  7. // Optional. Full path of a subdirectory to exclude from the total. 
  8. $exclude = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = recurse_dirsize($directory, $exclude); 
  12.  

Defined (1)

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

/wp-includes/ms-functions.php  
  1. function recurse_dirsize( $directory, $exclude = null ) { 
  2. $size = 0; 
  3.  
  4. $directory = untrailingslashit( $directory ); 
  5.  
  6. if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) { 
  7. return false; 
  8.  
  9. if ($handle = opendir($directory)) { 
  10. while(($file = readdir($handle)) !== false) { 
  11. $path = $directory.'/'.$file; 
  12. if ($file != '.' && $file != '..') { 
  13. if (is_file($path)) { 
  14. $size += filesize($path); 
  15. } elseif (is_dir($path)) { 
  16. $handlesize = recurse_dirsize( $path, $exclude ); 
  17. if ($handlesize > 0) 
  18. $size += $handlesize; 
  19. closedir($handle); 
  20. return $size;