<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_65128"> <div class="blockcode"> <div id="code_uAu"><ol> <li> </li> <li> <li>//代码也可以用于统计目录数</li> <li>//格式化输出目录大小 单位:Bytes,KB,MB,GB</li> <li> </li> <li>function getDirectorySize($path)</li> <li>{</li> <li> $totalsize = 0;</li> <li> $totalcount = 0;</li> <li> $dircount = 0;</li> <li> if ($handle = opendir ($path))</li> <li> {</li> <li> while (false !== ($file = readdir($handle)))</li> <li> {</li> <li> $nextpath = $path . '/' . $file;</li> <li> if ($file != '.' && $file != '..' && !is_link ($nextpath))</li> <li> {</li> <li> if (is_dir ($nextpath))</li> <li> {</li> <li> $dircount++;</li> <li> $result = getDirectorySize($nextpath);</li> <li> $totalsize += $result['size'];</li> <li> $totalcount += $result['count'];</li> <li> $dircount += $result['dircount'];</li> <li> }</li> <li> elseif (is_file ($nextpath))</li> <li> {</li> <li> $totalsize += filesize ($nextpath);</li> <li> $totalcount++;</li> <li> }</li> <li> }</li> <li> }</li> <li> }</li> <li> closedir ($handle);</li> <li> $total['size'] = $totalsize;</li> <li> $total['count'] = $totalcount;</li> <li> $total['dircount'] = $dircount;</li> <li> return $total;</li> <li>}</li> <li> </li> <li>function sizeFormat($size)</li> <li>{</li> <li> $sizeStr='';</li> <li> if($size {</li> <li> return $size." bytes";</li> <li> }</li> <li> else if($size {</li> <li> $size=round($size/1024,1);</li> <li> return $size." KB";</li> <li> }</li> <li> else if($size {</li> <li> $size=round($size/(1024*1024),1);</li> <li> return $size." MB";</li> <li> }</li> <li> else</li> <li> {</li> <li> $size=round($size/(1024*1024*1024),1);</li> <li> return $size." GB";</li> <li> }</li> <li> </li> <li>}</li> <li> </li> <li>$path="/home/www/htdocs";</li> <li>$ar=getDirectorySize($path);</li> <li> </li> <li>echo "<h4>路径 : $path</h4>";</li> <li>echo "目录大小 : ".sizeFormat($ar['size'])."<br>";</li> <li>echo "文件数 : ".$ar['count']."<br>";</li> <li>echo "目录术 : ".$ar['dircount']."<br>";</li> <li> </li> <li>//print_r($ar);</li> <li>?></li> <li> </li> <li> </ol></div> <em onclick="copycode($('code_uAu'));">复制代码</em> </div> <br> </td></tr></table> <div class="ptg mbm mtn"> PHP</div> <div id="comment_65128" class="cm"> </div> <div id="post_rate_div_65128"></div> <br><br>