isFile()) { $size += $file->getSize(); } } return $size; } /** * @param $bytes * @param $precision * @return string */ public static function formatBytes($bytes, $precision = 2): string { $units = array('B', 'KB', 'MB', 'GB', 'TB'); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= pow(1024, $pow); return round($bytes, $precision) . ' ' . $units[$pow]; } /** * @param $megabytes * @param $precision * @return string */ public static function formatMegaBytes($megabytes, $precision = 2): string { if($megabytes===-1){ return '∞'; } $bytes = $megabytes * pow(1024, 2); // Convert megabytes to bytes return self::formatBytes($bytes, $precision); } }