2013-03-03 68 views
26
function humanFileSize($size) 
{ 
    if ($size >= 1073741824) { 
     $fileSize = round($size/1024/1024/1024,1) . 'GB'; 
    } elseif ($size >= 1048576) { 
     $fileSize = round($size/1024/1024,1) . 'MB'; 
    } elseif($size >= 1024) { 
     $fileSize = round($size/1024,1) . 'KB'; 
    } else { 
     $fileSize = $size . ' bytes'; 
    } 
    return $fileSize; 
} 

...除了以下方面的工作很棒:我無法手動選擇我需要顯示的格式,比方說我只想用MB顯示文件大小。目前如果它在GB範圍內,它只會以GB顯示。人類可讀的文件大小

另外,如何限制小數點數爲2?

回答

42

嘗試這樣:

function humanFileSize($size,$unit="") { 
    if((!$unit && $size >= 1<<30) || $unit == "GB") 
    return number_format($size/(1<<30),2)."GB"; 
    if((!$unit && $size >= 1<<20) || $unit == "MB") 
    return number_format($size/(1<<20),2)."MB"; 
    if((!$unit && $size >= 1<<10) || $unit == "KB") 
    return number_format($size/(1<<10),2)."KB"; 
    return number_format($size)." bytes"; 
} 
+2

它應該是'$ unit ==「GB」|| !$ unit && $ size> = 1 << 30'。 – Gumbo 2013-03-03 17:01:25

+0

@Gumbo爲什麼?它有什麼不同?實際上,這會爲非字符串參數(如0)打破它。 – 2013-03-03 17:03:38

+0

您的解決方案要求該值至少爲單位大小的1,即無法獲得小於1的值,如「0.5 GB」。在這種情況下,結果以字節顯示。 – Gumbo 2013-03-03 17:18:35

0

您可以修改你的函數fullfil雙方你需要強制的單位,如果給定和調整精度。

function humanFileSize($size, $precision = 1, $show = "") 
{ 
    $b = $size; 
    $kb = round($size/1024, $precision); 
    $mb = round($kb/1024, $precision); 
    $gb = round($mb/1024, $precision); 

    if($kb == 0 || $show == "B") { 
     return $b . " bytes"; 
    } else if($mb == 0 || $show == "KB") { 
     return $kb . "KB"; 
    } else if($gb == 0 || $show == "MB") { 
     return $mb . "MB"; 
    } else { 
     return $gb . "GB"; 
    } 
} 

//Test with different values 
echo humanFileSize(1038) . "<br />";  
echo humanFileSize(103053, 0) . "<br />"; 
echo humanFileSize(103053) . "<br />"; 
echo humanFileSize(1030544553) . "<br />"; 
echo humanFileSize(1030534053405, 2, "GB") . "<br />"; ; 
1
function getHumanReadableSize($size, $unit = null, $decemals = 2) { 
    $byteUnits = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; 
    if (!is_null($unit) && !in_array($unit, $byteUnits)) { 
     $unit = null; 
    } 
    $extent = 1; 
    foreach ($byteUnits as $rank) { 
     if ((is_null($unit) && ($size < $extent <<= 10)) || ($rank == $unit)) { 
      break; 
     } 
    } 
    return number_format($size/($extent >> 10), $decemals) . $rank; 
} 

如果PHP版本低於5.4使用 $byteUnits = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');

1

我想這回的filesizes像Windows那樣的功能,令人驚奇我能找到沒有。更糟糕的是,這裏和其他地方的一些地方因爲假設1KB = 1000B而中斷了。

所以我編碼一個!另外還有兩個輔助功能。它們是:

// Returns a size in a human-readable form from a byte count. 
function humanSize($bytes) 
{ 
    if ($bytes < 1024) return "$bytes Bytes"; 

    $units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; 

    foreach ($units as $i => $unit) 
    { 
     // The reason for this threshold is to avoid e.g., "1000 KB", 
     // instead jumping from e.g., "999 KB" to "0.97 MB". 
     $multiplier = pow(1024, $i + 1); 
     $threshold = $multiplier * 1000; 

     if ($bytes < $threshold) 
     { 
      $size = formatToMinimumDigits($bytes/$multiplier, false); 
      return "$size $unit"; 
     } 
    } 
} 

// Efficiently calculates how many digits the integer portion of a number has. 
function digits($number) 
{ 
    // Yes, I could convert to string and count the characters, 
    // but this is faster and cooler. 
    $log = log10($number); 
    if ($log < 0) return 1; 
    return floor($log) + 1; 
} 

// Formats a number to a minimum amount of digits. 
// In other words, makes sure that a number has at least $digits on it, even if 
// that means introducing redundant decimal zeroes at the end, or rounding the 
// ones present exceeding the $digits count when combined with the integers. 
// For example: 
//  formatToMinimumDigits(10)   // 10.0 
//  formatToMinimumDigits(1.1)   // 1.10 
//  formatToMinimumDigits(12.34)  // 12.3 
//  formatToMinimumDigits(1.234)  // 1.23 
//  formatToMinimumDigits(1.203)  // 1.20 
//  formatToMinimumDigits(123.4)  // 123 
//  formatToMinimumDigits(100)   // 100 
//  formatToMinimumDigits(1000)   // 1000 
//  formatToMinimumDigits(1)   // 1.00 
//  formatToMinimumDigits(1.002)  // 1.00 
//  formatToMinimumDigits(1.005)  // 1.01 
//  formatToMinimumDigits(1.005, false) // 1.00 
// This is primarily useful for generating human-friendly numbers. 
function formatToMinimumDigits($value, $round = true, $digits = 3) 
{ 
    $integers = floor($value); 

    $decimalsNeeded = $digits - digits($integers); 

    if ($decimalsNeeded < 1) 
    { 
     return $integers; 
    } 
    else 
    { 
     if ($round) 
     { 
      // This relies on implicit type casting of float to string. 
      $parts = explode('.', round($value, $decimalsNeeded)); 
      // We re-declare the integers because they may change 
      // after we round the number. 
      $integers = $parts[0]; 
     } 
     else 
     { 
      // Again, implicit type cast to string. 
      $parts = explode('.', $value); 
     } 

     // And because of the implicit type cast, we must guard against 
     // 1.00 becoming 1, thus not exploding the second half of it. 
     $decimals = isset($parts[1]) ? $parts[1] : '0'; 
     $joined = "$integers.$decimals".str_repeat('0', $digits); 
     return substr($joined, 0, $digits + 1); 
    } 
} 

用法與humanSize(123456789)一樣簡單。

31

有由Jeffrey Sambells就是很好的例子:

function human_filesize($bytes, $dec = 2) 
{ 
    $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); 
    $factor = floor((strlen($bytes) - 1)/3); 

    return sprintf("%.{$dec}f", $bytes/pow(1024, $factor)) . @$size[$factor]; 
} 

print human_filesize(filesize('example.zip')); 
+1

不錯,但我會加 'if($ factor == 0)$ dec = 0;' – 2014-11-20 18:37:17

+0

它不準確,4,281,016字節給4.08 MB,而谷歌說4.28102兆 – AMB 2015-03-31 12:58:09

+0

我猜google是錯誤tinking 1mb = 1000000 byes,它應該b 1024 * 1024 – AMB 2015-03-31 13:02:09

7

我用這個方法:

function byteConvert($bytes) 
{ 
    if ($bytes == 0) 
     return "0.00 B"; 

    $s = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); 
    $e = floor(log($bytes, 1024)); 

    return round($bytes/pow(1024, $e), 2).$s[$e]; 
} 

工作在鄰大(1)。

+0

這是工作很好, – AMB 2017-11-06 14:57:06

0

這裏是一個工作的功能管理,直到堯字節:

function DisplayFileSize($size, $unit = false, $precision = 2){ 

$b = $size; 
$kb = round($size/1024, $precision); 
$mb = round($kb/1024, $precision); 
$gb = round($mb/1024, $precision); 
$tb = round($gb/1024, $precision); 
$pb = round($tb/1024, $precision); 
$eb = round($pb/1024, $precision); 
$zb = round($eb/1024, $precision); 
$yb = round($zb/1024, $precision); 

if((!$unit && floor($kb) == 0) || $unit == "b") { 
    return array("value" => FormatNumber($b), "unit" => "bytes"); 
} else if((!$unit && floor($mb) == 0) || $unit == "kb") { 
    return array("value" => FormatNumber($kb, 2), "unit" => "Kb"); 
} else if((!$unit && floor($gb) == 0) || $unit == "mb") { 
    return array("value" => FormatNumber($mb, 2), "unit" => "Mb"); 
} else if((!$unit && floor($tb) == 0) || $unit == "gb") { 
    return array("value" => FormatNumber($gb, 2), "unit" => "Gb"); 
} else if((!$unit && floor($pb) == 0) || $unit == "tb") { 
    return array("value" => FormatNumber($tb, 2), "unit" => "Tb"); 
} else if((!$unit && floor($eb) == 0) || $unit == "pb") { 
    return array("value" => FormatNumber($pb, 2), "unit" => "Pb"); 
} else if((!$unit && floor($zb) == 0) || $unit == "eb") { 
    return array("value" => FormatNumber($eb, 2), "unit" => "Eb"); 
} else if((!$unit && floor($yb) == 0) || $unit == "zb") { 
    return array("value" => FormatNumber($zb, 2), "unit" => "Zb"); 
} else { 
    return array("value" => FormatNumber($yb, 2), "unit" => "Yb"); 
} 

}

3

要擴大Vaidas'的答案,這裏是你應該怎麼做才能說明新IEC標準:

function human_readable_bytes($bytes, $decimals = 2, $system = 'binary') 
{ 
    $mod = ($system === 'binary') ? 1024 : 1000; 

    $units = array(
     'binary' => array(
      'B', 
      'KiB', 
      'MiB', 
      'GiB', 
      'TiB', 
      'PiB', 
      'EiB', 
      'ZiB', 
      'YiB', 
     ), 
     'metric' => array(
      'B', 
      'kB', 
      'MB', 
      'GB', 
      'TB', 
      'PB', 
      'EB', 
      'ZB', 
      'YB', 
     ), 
    ); 

    $factor = floor((strlen($bytes) - 1)/3); 

    return sprintf("%.{$decimals}f%s", $bytes/pow($mod, $factor), $units[$system][$factor]); 
} 

從技術上講,根據存儲設備等的規格,您應該使用公制系統作爲默認值(這就是爲什麼Google轉換器顯示kB - > MB爲mod 1000而不是1024)。

+0

$大小應該是$字節 – PSSGCSim 2017-01-16 23:38:34

+0

固定。感謝您的注意。 – 2017-01-17 14:16:35