A WordPress-centric search engine for devs and theme authors



recurse_dirsize ›

Since4.3.0
Deprecatedn/a
recurse_dirsize ( $directory, $exclude = null )
Parameters: (2)
  • (string) $directory Full path of a directory.
    Required: Yes
  • (string) $exclude Optional. Full path of a subdirectory to exclude from the total.
    Required: No
    Default: null
Returns:
  • (int|false) Size in MB if a valid directory. False if not.
Defined at:
Codex:
Change Log:
  • MU

Get the size of a directory recursively.

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



Source

function recurse_dirsize( $directory, $exclude = null ) {
	$size = 0;

	$directory = untrailingslashit( $directory );

	if ( ! file_exists( $directory ) || ! is_dir( $directory ) || ! is_readable( $directory ) || $directory === $exclude ) {
		return false;
	}

	if ( $handle = opendir( $directory ) ) {
		while ( ( $file = readdir( $handle ) ) !== false ) {
			$path = $directory . '/' . $file;
			if ( $file != '.' && $file != '..' ) {
				if ( is_file( $path ) ) {
					$size += filesize( $path );
				} elseif ( is_dir( $path ) ) {
					$handlesize = recurse_dirsize( $path, $exclude );
					if ( $handlesize > 0 ) {
						$size += $handlesize;
					}
				}
			}
		}
		closedir( $handle );
	}
	return $size;
}