PHP檔案大小格式化函數合集_php執行個體

來源:互聯網
上載者:User

比如碰到一個很大的檔案有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()函數獲得檔案的位元組數,再用上面的這些函數換算成適當的單位就可以了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.