diff --git a/src/Library/Util.php b/src/Library/Util.php index 33f25af..80a520f 100644 --- a/src/Library/Util.php +++ b/src/Library/Util.php @@ -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'; + } }