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

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

WBOY
WBOYOriginal
2016-06-07 11:44:351395Durchsuche

计算文件夹的大小,包括子文件夹,格式化输出文件夹大小、文件数、子文件夹数信息。
<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元

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn