php 檔案目錄大小統計函數(自動計算Bytes,KB,MB,GB)

來源:互聯網
上載者:User

計算檔案夾的大小,包括子檔案夾,格式化輸出檔案夾大小、檔案數、子檔案夾數資訊。

 代碼如下 複製代碼

<?
//代碼也可以用於統計目錄數
//格式化輸出目錄大小 單位:Bytes,KB,MB,GB
 
function getDirectorySize($path)
{
  $totalsize = 0;
  $totalcount = 0;
  $dircount = 0;
  if ($handle = opendir ($path))
  {
    while (false !== ($file = readdir($handle)))
    {
      $nextpath = $path . '/' . $file;
      if ($file != '.' && $file != '..' && !is_link ($nextpath))
      {
        if (is_dir ($nextpath))
        {
          $dircount++;
          $result = getDirectorySize($nextpath);
          $totalsize += $result['size'];
          $totalcount += $result['count'];
          $dircount += $result['dircount'];
        }
        elseif (is_file ($nextpath))
        {
          $totalsize += filesize ($nextpath);
          $totalcount++;
        }
      }
    }
  }
  closedir ($handle);
  $total['size'] = $totalsize;
  $total['count'] = $totalcount;
  $total['dircount'] = $dircount;
  return $total;
}

PHP中計算檔案目錄大小其實主要是用到"filesize"函數,通過遞迴的方法計算每個檔案的大小,再計算他們的和即是整個檔案目錄的大小。

因為直接返回的檔案大小是以位元組為單位的,所以我們一般還要經過換算得到我們常見得大小,以下是單位換算的函數:

 代碼如下 複製代碼
 
function sizeFormat($size)
{
    $sizeStr='';
    if($size<1024)
    {
        return $size." bytes";
    }
    else if($size<(1024*1024))
    {
        $size=round($size/1024,1);
        return $size." KB";
    }
    else if($size<(1024*1024*1024))
    {
        $size=round($size/(1024*1024),1);
        return $size." MB";
    }
    else
    {
        $size=round($size/(1024*1024*1024),1);
        return $size." GB";
    }
 
}
 
$path="/home/www/htdocs";
$ar=getDirectorySize($path);
 
echo "<h4>路徑 : $path</h4>";
echo "目錄大小 : ".sizeFormat($ar['size'])."<br>";
echo "檔案數 : ".$ar['count']."<br>";
echo "目錄術 : ".$ar['dircount']."<br>";
 
//print_r($ar);
?>


後面附一個單位函數

該函數最主要的是根據檔案的位元組數,判斷應當選擇的統計單位,也就是說一個檔案用某一單位比如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;
}

function round_dp($num , $dp)
{
  $sh = pow(10 , $dp);
  return (round($num*$sh)/$sh);
}

關於php round函數用法可參考 http://www.111cn.net/w3school/php/func_math_round.htm

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.