php如何擷取檔案夾大小程式代碼

來源:互聯網
上載者:User
 代碼如下 複製代碼

<?php
 
//擷取檔案夾大小
function dir_size($dir) {
    if (!preg_match('#/$#', $dir)) {
        $dir .= '/';
    }
    $totalsize = 0;
    //調用檔案清單
    foreach (get_file_list($dir) as $name) {
        $totalsize += (@is_dir($dir.$name) ? dir_size("$dir$name/") :
            (int)@filesize($dir.$name));
    }
    return $totalsize;
}
 
//擷取檔案清單
function get_file_list($path) {
    $f = $d = array();
    //擷取所有檔案
    foreach (get_all_files($path) as $name) {
        if (@is_dir($path.$name)) {
            $d[] = $name;
        } else if (@is_file($path.$name)) {
            $f[] = $name;
        }
    }
    natcasesort($d);
    natcasesort($f);
    return array_merge($d, $f);
}
 
//擷取所有檔案
function get_all_files($path) {
    $list = array();
    if (($hndl = @opendir($path)) === false) {
        return $list;
    }
    while (($file=readdir($hndl)) !== false) {
        if ($file != '.' && $file != '..') {
            $list[] = $file;
        }
    }
    closedir($hndl);
    return $list;
}
 
//轉換單位
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];
}
 
//目錄
$path = './test_dir/';
 
//顯示檔案清單
print_r(get_file_list($path)).'<br>';
 
//顯示檔案大小
echo dir_size($path).'<br>';
 
//顯示轉換過單位的大小
echo setupSize(dir_size($path));
 
?>

dir_size() 是擷取檔案夾大小的函數,函數中使用了遞迴的方法,此函數需要調用 get_file_list() ,擷取檔案夾中的所有檔案清單,如果檔案清單中有檔案夾存在,就調用 get_all_files() 擷取檔案夾下的檔案清單。由此取得目標檔案夾的大小。

setupSize() 是將傳人的大小轉換為易於讀取的檔案單位,可以轉換成 Bytes, KB, MB, GB, TB, PB, EB, ZB, YB 等單位。

列2

 代碼如下 複製代碼

<?php

function getDirSize($dir)
    {
        $handle = opendir($dir);
        while (false!==($FolderOrFile = readdir($handle)))
        {
            if($FolderOrFile != "." && $FolderOrFile != "..")
            {
                if(is_dir("$dir/$FolderOrFile"))
                {
                    $sizeResult += getDirSize("$dir/$FolderOrFile");
                }
                else
                {
                    $sizeResult += filesize("$dir/$FolderOrFile");
                }
            }   
        }
        closedir($handle);
        return $sizeResult;
    }

    // 單位自動轉換函式
    function getRealSize($size)
    {
        $kb = 1024;         // Kilobyte
        $mb = 1024 * $kb;   // Megabyte
        $gb = 1024 * $mb;   // Gigabyte
        $tb = 1024 * $gb;   // Terabyte
       
        if($size < $kb)
        {
            return $size." B";
        }
        else if($size < $mb)
        {
            return round($size/$kb,2)." KB";
        }
        else if($size < $gb)
        {
            return round($size/$mb,2)." MB";
        }
        else if($size < $tb)
        {
            return round($size/$gb,2)." GB";
        }
        else
        {
            return round($size/$tb,2)." TB";
        }
    }

    echo getRealSize(getDirSize(dirname($_SERVER[SCRIPT_FILENAME])./include/));


?>

#########################################################

//function dirsize($dir)
//{
//   $handle=opendir($dir);
//   $size = 0;
//   while ( $file=readdir($handle) )
//   {
//       if ( ( $file == "." ) || ( $file == ".." ) ) continue;
//       if ( is_dir("$dir/$file") )
//           $size += dirsize("$dir/$file");
//       else
//           $size += filesize("$dir/$file");
//   }
//   closedir($handle);
//   return $size;
//}
//$big=dirsize(dirname($_SERVER[SCRIPT_FILENAME])."/");
//echo $big;

得到的結果是小數點後兩位的

$big*1024 得到單位為KB

聯繫我們

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