辅助计算容量大小

支持获取文件夹大小
支持传入B、MB进行合适的单位转换
This commit is contained in:
Chenx221 2024-03-02 19:16:05 +08:00
parent c17c8ac74a
commit f3119ac609
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021

59
utils/FileSizeHelper.php Normal file
View File

@ -0,0 +1,59 @@
<?php
namespace app\utils;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
class FileSizeHelper
{
/**
* 计算指定目录的大小
*
* @param string $directory 目录路径
* @return int 目录的大小(字节)
*/
public static function getDirectorySize(string $directory): int
{
$size = 0;
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach ($files as $file) {
if ($file->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);
}
}