php中有一個系統內建的計算檔案大小的函數,就是filesize(),但是這個函數是以位元組為單位的,在一些情況下,我們需要很直觀的瞭解一個檔案大小,就不僅僅需要位元組B這個單位了,還需要KB,MB,GB,甚至更大TB,PB,所以我們需要自己寫一些函數來格式化filesize()的輸出結果
比如碰到一個很大的檔案有49957289167B,大家一看這麼一長串的數字後面單位是位元組B,還是不知道這個檔案的大小是一個什麼概念,我們把它轉換成GB為單位,就是46.53GB。用下面這些函數就可以完成這個工作: 代碼如下://轉換單位function setupSize($fileSize) { $size = sprintf("%u", $fileSize); if($size == 0) { return("0 Bytes"); } $sizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"); return round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizename[$i];}function byte_format($size, $dec=2){ $a = array("B", "KB", "MB", "GB", "TB", "PB"); $pos = 0; while ($size >= 1024) { $size /= 1024; $pos++; } return round($size,$dec)." ".$a[$pos]; }/* Use : echo format_size(filesize("fichier"));Example result : 13,37 Ko */ function format_size($o) { $size = array('Go' => 1073741824, 'Mo' => 1048576, 'Ko' => 1024, 'octets' => 1); foreach ($size as $k => $v) if ($o >= $v) { if ($k == 'octets') return round($o).' '.$k; return number_format($o / $v, 2, ',', ' ').' '.$k; }}/** * 檔案大小格式化 * @param integer $size 初始檔案大小,單位為byte * @return array 格式化後的檔案大小和單位元組,單位為byte、KB、MB、GB、TB */function file_size_format($size = 0, $dec = 2) { $unit = array("B", "KB", "MB", "GB", "TB", "PB"); $pos = 0; while ($size >= 1024) { $size /= 1024; $pos++; } $result['size'] = round($size, $dec); $result['unit'] = $unit[$pos]; return $result['size'].$result['unit'];}echo file_size_format(123456789); /** * 檔案大小單位格式化 * @param $bytes 檔案實際大小,單位byte * @param $prec 轉換後精確度,預設精確到小數點後兩位 * @return 轉換後的大小+單位的字串 */ function fsizeformat($bytes,$prec=2){ $rank=0; $size=$bytes; $unit="B"; while($size>1024){ $size=$size/1024; $rank++; } $size=round($size,$prec); switch ($rank){ case "1": $unit="KB"; break; case "2": $unit="MB"; break; case "3": $unit="GB"; break; case "4": $unit="TB"; break; default : } return $size." ".$unit; } /** * 容量格式化 * @param String 檔案名稱(檔案路徑) * @return 如果檔案存在返回格式化的字串 如果錯誤返回錯誤資訊 Unknown File */ function sizeFormat ($fileName){ //擷取檔案的大小 @ $filesize=filesize($fileName); //如果檔案不存在返回錯誤資訊 if(false==$filesize){ return 'Unknown File'; } //格式檔案容量資訊 if ($filesize >= 1073741824) $filesize = round($filesize / 1073741824 * 100) / 100 . ' GB'; elseif ($filesize >= 1048576) $filesize = round($filesize / 1048576 * 100) / 100 . ' MB'; elseif ($filesize >= 1024) $filesize = round($filesize / 1024 * 100) / 100 . ' KB'; else $filesize = $filesize . ' Bytes'; return $filesize; }//測試函數 echo sizeFormat('config.inc.php'); /** * 檔案大小格式化 * @param type $filesize */private function sizeCount($filesize){ if ($filesize >= 1073741824) { $filesize = round($filesize / 1073741824 * 100) / 100 . ' GB'; } else if ($filesize >= 1048576) { $filesize = round($filesize / 1048576 * 100) / 100 . ' MB'; } else if ($filesize >= 1024) { $filesize = round($filesize / 1024 * 100) / 100 . ' KB'; } return $filesize;} //該函數最主要的是根據檔案的位元組數,判斷應當選擇的統計單位,也就是說一個檔案用某一單位比如MB,那麼該檔案肯定小於1GB,否則當然要用GB作為單位了,而且檔案要大於KB,不然的話得用更小的單位統計。該函數代碼如下 //size() 統計檔案大小function size($byte){ if($byte < 1024) { $unit="B"; } else if($byte < 10240) { $byte=round_dp($byte/1024, 2); $unit="KB"; } else if($byte < 102400) { $byte=round_dp($byte/1024, 2); $unit="KB"; } else if($byte < 1048576) { $byte=round_dp($byte/1024, 2); $unit="KB"; } else if ($byte < 10485760) { $byte=round_dp($byte/1048576, 2); $unit="MB"; } else if ($byte < 104857600) { $byte=round_dp($byte/1048576,2); $unit="MB"; } else if ($byte < 1073741824) { $byte=round_dp($byte/1048576, 2); $unit="MB"; } else { $byte=round_dp($byte/1073741824, 2); $unit="GB"; } $byte .= $unit; return $byte;}//輔助函數 round_up(),該函數用來取捨小數點位元的,四捨五入。function round_dp($num , $dp){ $sh = pow(10 , $dp); return (round($num*$sh)/$sh);} 這樣我們就能很好額統計任何一個檔案的大小了,首先通過系統內建的filesize()函數獲得檔案的位元組數,再用上面的這些函數換算成適當的單位就可以了