WordPress at a glance

recurse_dirsize() WP 1.0

Get the size of a directory recursively.

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

  • Since 4.3.0 $exclude parameter added.
Is the basis for: get_dirsize()

No Hooks.

Return

Int/false. Size in MB if a valid directory. False if not.

Usage

recurse_dirsize( $directory, $exclude );
$directory(string) (required)
Full path of a directory.
$exclude(string)
Full path of a subdirectory to exclude from the total.
Default: null

Code of recurse_dirsize: wp-includes/ms-functions.php VER 4.9.8

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

Related Functions

From tag: File system (create delete files folders)

More from category: Multisite

No comments
    Hello, !     Log In . Register