recurse_dirsize( string $directory, string $exclude = null )

Get the size of a directory recursively.


Description Description

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


Parameters Parameters

$directory

(string) (Required) Full path of a directory.

$exclude

(string) (Optional) Full path of a subdirectory to exclude from the total.

Default value: null


Top ↑

Return Return

(int|false) Size in MB if a valid directory. False if not.


Top ↑

Source Source

File: wp-includes/ms-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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
MU (3.0.0) MU (3.0.0)
4.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.