Sử dụng php tính tổng số file, dung lượng của các thư mục

Hàm đếm số file và tính tổng dung lượng theo 1 đường dẫn thư mục.

  1. <br />  
  2.  function getDirectorySize($path)<br />  
  3. {<br />  
  4.     $totalsize = 0;<br />  
  5.     $totalcount = 0;<br />  
  6.     $dircount = 0;<br />  
  7.     if ($handle = opendir ($path))<br />  
  8.     {<br />  
  9.         while (false !== ($file = readdir($handle)))<br />  
  10.         {<br />  
  11.             $nextpath = $path . ‘/’ . $file;<br />  
  12.             if ($file != ‘.’ && $file != ‘..’ && !is_link ($nextpath))<br />  
  13.             {<br />  
  14.                 if (is_dir ($nextpath))<br />  
  15.                 {<br />  
  16.                     $dircount++;<br />  
  17.                     $result = getDirectorySize($nextpath);<br />  
  18.                     $totalsize += $result[‘size’];<br />  
  19.                     $totalcount += $result[‘count’];<br />  
  20.                     $dircount += $result[‘dircount’];<br />  
  21.                 }<br />  
  22.                 elseif (is_file ($nextpath))<br />  
  23.                 {<br />  
  24.                     $totalsize += filesize ($nextpath);<br />  
  25.                     $totalcount++;<br />  
  26.                 }<br />  
  27.             }<br />  
  28.         }<br />  
  29.     }<br />  
  30.     closedir ($handle);<br />  
  31.     $total[‘size’] = $totalsize;<br />  
  32.     $total[‘count’] = $totalcount;<br />  
  33.     $total[‘dircount’] = $dircount;<br />  
  34.     return $total;<br />  
  35. }</p>  
  36. <p>function sizeFormat($size)<br />  
  37. {<br />  
  38.     if($size<1024)  
  39.     {  
  40.         return $size." bytes";  
  41.     }  
  42.     else if($size<(1024*1024))  
  43.     {  
  44.         $size=round($size/1024,1);  
  45.         return $size." KB";  
  46.     }  
  47.     else if($size<(1024*1024*1024))  
  48.     {  
  49.         $size=round($size/(1024*1024),1);  
  50.         return $size." MB";  
  51.     }  
  52.     else  
  53.     {  
  54.         $size=round($size/(1024*1024*1024),1);  
  55.         return $size." GB";  
  56.     }  
  57.   
  58. }  

Duyệt các thư mục con trong thư mục chính và gọi hàm getDirectorySize() ở trên.

  1.  function folderScan($path) {<br />  
  2. $total_size = 0;<br />  
  3. $files = scandir($path);<br />  
  4. $cleanPath = rtrim($path, ‘/’). ‘/’;</p>  
  5. <p>foreach($files as $t) {<br />  
  6. if ($t<>”.” && $t<>”..”) {<br />  
  7. $currentFile = $cleanPath . $t;<br />  
  8. if (is_dir($currentFile)) {<br />  
  9. $ar = getDirectorySize($currentFile);</p>  
  10. <p>echo “<h4>Details for the path : $currentFile</h4>”;<br />  
  11. echo “Total size : “.sizeFormat($ar[‘size’]).”<br>”;<br />  
  12. echo “No. of files : “.$ar[‘count’].”<br>”;<br />  
  13. echo “No. of directories : “.$ar[‘dircount’].”<br>”;<br />  
  14. }</p>  
  15. <p>}<br />  
  16. }<br />  
  17. }  

Thiết lập đường dẫn thư mục chính và chạy chương trình.
  1. <br />  
  2. $path=”/home/name/public_html”;folderScan($path);<br />  

 

Leave a Reply

Your email address will not be published.