recurse_dirsize( string $directory, string $exclude = null )
Get the size of a directory recursively.
Description
Used by get_dirsize() to get a directory’s 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.
Default value: null
Return
(int|false) Size in MB if a valid directory. False if not.
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;
}
Changelog
Version | Description |
---|---|
WP-4.3.0 | $exclude parameter added. |
WP-MU | Introduced. (3.0.0) |