/** * Get the size of a directory recursively. * * Used by get_dirsize() to get a directory's size when it contains * other directories. * * @since MU * @since 4.3.0 $exclude parameter added. * * @param string $directory Full path of a directory. * @param string $exclude Optional. Full path of a subdirectory to exclude from the total. * @return int|false Size in MB if a valid directory. False if not. */ 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; }
/** * Get the size of a directory recursively. * * Used by get_dirsize() to get a directory's size when it contains * other directories. * * @since MU * * @param string $directory * @return int */ function recurse_dirsize($directory) { $size = 0; if (substr($directory, -1) == '/') { $directory = substr($directory, 0, -1); } if (!file_exists($directory) || !is_dir($directory) || !is_readable($directory)) { 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); if ($handlesize > 0) { $size += $handlesize; } } } } closedir($handle); } return $size; }
public function filter_get_directory_size($size, $path) { require_once ABSPATH . 'wp-includes/ms-functions.php'; if (!is_dir($path)) { $size = -1; } else { $size = recurse_dirsize($path); } return $size; }