recurse_dirsize( string $directory, string|string[] $exclude = null, int $max_execution_time = null, array $directory_cache = null )

Gets the size of a directory recursively.


Description

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


Parameters

$directory

(Required) Full path of a directory.

$exclude

(Optional) Full path of a subdirectory to exclude from the total, or array of paths. Expected without trailing slash(es).<br>

Default value: null

$max_execution_time

(Optional) Maximum time to run before giving up. In seconds.<br> The timeout is global and is measured from the moment WordPress started to load. Defaults to the value of max_execution_time PHP setting.

Default value: null

$directory_cache

(Optional) Array of cached directory paths.<br> Defaults to the value of dirsize_cache transient.

Default value: null


Return

(int|false|null) Size in bytes if a valid directory. False if not. Null if timeout.


Source

File: wp-includes/functions.php

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


Changelog

Changelog
Version Description
5.6.0 The $directory_cache parameter was added.
5.2.0 The $max_execution_time parameter was added.
4.3.0 The $exclude parameter was added.
MU (3.0.0) Introduced. MU (3.0.0)