From 76dc5308a188ef78b64a12af7ab3ca3f4cce2b45 Mon Sep 17 00:00:00 2001 From: zj Date: Thu, 6 Aug 2026 15:53:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=97=E8=8A=82=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=98=93=E8=AF=BB=E5=8D=95?= =?UTF-8?q?=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Library/Util.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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'; + } }