Home  >  Article  >  php教程  >  计算指定文件夹的信息(文件夹数,文件数,文件夹大小)

计算指定文件夹的信息(文件夹数,文件数,文件夹大小)

WBOY
WBOYOriginal
2016-06-07 11:44:351394browse

计算文件夹的大小,包括子文件夹,格式化输出文件夹大小、文件数、子文件夹数信息。
<br> //代码也可以用于统计目录数<br> //格式化输出目录大小 单位:Bytes,KB,MB,GB<br>  <br> function getDirectorySize($path)<br> {<br>   $totalsize = 0;<br>   $totalcount = 0;<br>   $dircount = 0;<br>   if ($handle = opendir ($path))<br>   {<br>     while (false !== ($file = readdir($handle)))<br>     {<br>       $nextpath = $path . '/' . $file;<br>       if ($file != '.' && $file != '..' && !is_link ($nextpath))<br>       {<br>         if (is_dir ($nextpath))<br>         {<br>           $dircount++;<br>           $result = getDirectorySize($nextpath);<br>           $totalsize += $result['size'];<br>           $totalcount += $result['count'];<br>           $dircount += $result['dircount'];<br>         }<br>         elseif (is_file ($nextpath))<br>         {<br>           $totalsize += filesize ($nextpath);<br>           $totalcount++;<br>         }<br>       }<br>     }<br>   }<br>   closedir ($handle);<br>   $total['size'] = $totalsize;<br>   $total['count'] = $totalcount;<br>   $total['dircount'] = $dircount;<br>   return $total;<br> }<br>  <br> function sizeFormat($size)<br> {<br>     $sizeStr='';<br>     if($size     {<br>         return $size." bytes";<br>     }<br>     else if($size     {<br>         $size=round($size/1024,1);<br>         return $size." KB";<br>     }<br>     else if($size     {<br>         $size=round($size/(1024*1024),1);<br>         return $size." MB";<br>     }<br>     else<br>     {<br>         $size=round($size/(1024*1024*1024),1);<br>         return $size." GB";<br>     }<br>  <br> }<br>  <br> $path="/home/www/htdocs";<br> $ar=getDirectorySize($path);<br>  <br> echo "<h4>路径 : $path</h4>";<br> echo "目录大小 : ".sizeFormat($ar['size'])."<br>";<br> echo "文件数 : ".$ar['count']."<br>";<br> echo "目录术 : ".$ar['dircount']."<br>";<br>  <br> //print_r($ar);<br> ?>

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn