新增字节自动格式化易读单位

This commit is contained in:
2026-08-06 15:53:59 +08:00
parent ff7adab00d
commit 76dc5308a1

View File

@@ -387,4 +387,27 @@ final class Util
return [$content, $filePath];
}
/**
* 字节自动格式化易读单位
* @param float $bytes
* @return string
*/
public static function prettyBytes(float $bytes): string
{
$step = pow(1024, 4);
if ($bytes >= $step) {
return round($bytes / $step, 2) . 'TB';
}
$step = pow(1024, 3);
if ($bytes >= $step) {
return round($bytes / $step, 2) . 'GB';
}
$step = pow(1024, 2);
if ($bytes >= $step) {
return round($bytes / $step, 2) . 'MB';
}
$step = 1024;
return round($bytes / $step, 2) . 'KB';
}
}