資料庫或 filesize() 回傳原始位元組數(如 49957289167),人類難以直觀判斷大小。格式化為 46.53 GB 才具可讀性。
function setupSize($fileSize) {
$size = sprintf("%u", $fileSize); // 確保無符號整數
if ($size == 0) return "0 Bytes";
$units = [" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"];
$i = floor(log($size, 1024));
return round($size / pow(1024, $i), 2) . $units[$i];
}
特點:數學推導單位索引、支援到 YB (1024^8)、程式碼最短。
function byte_format($size, $dec = 2) {
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
$pos = 0;
while ($size >= 1024 && $pos < count($units) - 1) {
$size /= 1024;
$pos++;
}
return round($size, $dec) . ' ' . $units[$pos];
}
特點:邏輯清晰、易維護、可控制小數位。
function format_size($bytes) {
$units = ['Go' => 1073741824, 'Mo' => 1048576, 'Ko' => 1024, 'octets' => 1];
foreach ($units as $unit => $threshold) {
if ($bytes >= $threshold) {
if ($unit === 'octets') return round($bytes) . ' ' . $unit;
return number_format($bytes / $threshold, 2, ',', ' ') . ' ' . $unit;
}
}
}
特點:法語單位 (Go/Mo/Ko)、使用 number_format 千分位分隔。
function file_size_format($size = 0, $dec = 2) {
$units = ["B", "KB", "MB", "GB", "TB", "PB"];
$pos = 0;
while ($size >= 1024 && $pos < count($units) - 1) {
$size /= 1024;
$pos++;
}
return [
'size' => round($size, $dec),
'unit' => $units[$pos],
'formatted' => round($size, $dec) . ' ' . $units[$pos]
];
}
// 使用
$info = file_size_format(123456789);
echo $info['formatted']; // 117.74 MB
echo $info['size']; // 117.74
echo $info['unit']; // MB
function fsizeformat($bytes, $prec = 2) {
$rank = 0;
$size = $bytes;
while ($size > 1024) {
$size /= 1024;
$rank++;
}
$size = round($size, $prec);
$unit = ['B', 'KB', 'MB', 'GB', 'TB'][$rank] ?? 'PB';
return $size . ' ' . $unit;
}
function sizeFormat($fileName) {
$filesize = @filesize($fileName);
if ($filesize === false) return 'Unknown File';
if ($filesize >= 1073741824) return round($filesize / 1073741824, 2) . ' GB';
if ($filesize >= 1048576) return round($filesize / 1048576, 2) . ' MB';
if ($filesize >= 1024) return round($filesize / 1024, 2) . ' KB';
return $filesize . ' Bytes';
}
function size($byte) {
if ($byte < 1024) return $byte . ' B';
if ($byte < 10240) return round($byte / 1024, 2) . ' KB';
if ($byte < 102400) return round($byte / 1024, 2) . ' KB';
if ($byte < 1048576) return round($byte / 1024, 2) . ' KB';
if ($byte < 10485760) return round($byte / 1048576, 2) . ' MB';
if ($byte < 104857600) return round($byte / 1048576, 2) . ' MB';
if ($byte < 1073741824) return round($byte / 1048576, 2) . ' MB';
return round($byte / 1073741824, 2) . ' GB';
}
特點:針對不同量級給予不同精度(小檔案不顯示小數、大檔案保留兩位)。
| 版本 | 效能 | 適用場景 | 推薦指數 |
|---|---|---|---|
| 對數運算版 | 高 | 通用、支援超大單位 | ⭐⭐⭐⭐⭐ |
| 迴圈除法版 | 高 | 通用、易維護 | ⭐⭐⭐⭐⭐ |
| 結構化回傳版 | 高 | 需分離數值/單位的 API | ⭐⭐⭐⭐ |
| Switch 版 | 最高 | 極高頻呼叫、熱路徑 | ⭐⭐⭐⭐ |
| 區間判斷版 | 中 | 需差異化精度顯示 | ⭐⭐⭐ |
/**
* 檔案大小人性化格式化
* @param int|string $bytes 位元組數
* @param int $decimals 小數位數
* @param bool $si 是否使用 SI 標準 (1000) 而非 IEC (1024)
* @return string
*/
function human_filesize($bytes, $decimals = 2, $si = false) {
$bytes = (float) $bytes;
if ($bytes <= 0) return '0 B';
$base = $si ? 1000 : 1024;
$units = $si
? ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
$exp = floor(log($bytes, $base));
$exp = min($exp, count($units) - 1);
$value = $bytes / pow($base, $exp);
return round($value, $decimals) . ' ' . $units[$exp];
}
// 測試
echo human_filesize(49957289167); // 46.53 GiB (IEC 標準,預設)
echo human_filesize(49957289167, 2, true); // 49.96 GB (SI 標準,硬碟廠商慣用)
此版本同時支援 IEC 標準 (1024,KiB/MiB) 與 SI 標準 (1000,kB/MB),符合國際規範,生產環境強烈推薦。