PHP遍历目录返回统计目录大小实例_PHP教程

WBOY
发布: 2016-07-13 10:28:45
原创
823 人浏览过

分享一个 PHP遍历目录并返回统计目录大小的方法。
代码:

<?<span>php 
</span><span>$dirname</span> = "test1"<span>; 
</span><span>//</span><span>mkdir($dirname);

//遍历一层目录 </span>
<span>function</span> listdir(<span>$dirname</span><span>) { 
</span><span>$ds</span> = <span>opendir</span>(<span>$dirname</span><span>); 
</span><span>while</span>(<span>$file</span> = <span>readdir</span>(<span>$ds</span><span>)) { 
</span><span>$path</span> = <span>$dirname</span>.'/'.<span>$file</span><span>; 
</span><span>if</span>(<span>is_dir</span>(<span>$file</span><span>)) { 
</span><span>echo</span> "DIR:".<span>$file</span>."<br>"<span>; 
</span><span>if</span>(<span>$file</span> != "." && <span>$file</span> != ".."<span>) { 
listdir(</span><span>$file</span><span>); 
} 
} 
</span><span>else</span><span> { 
</span><span>echo</span> "FILE:".<span>$file</span> . "<br>"<span>; 
} 
} 
}

</span><span>function</span> totdir(<span>$dirname</span>) { <span>//</span><span>对listdir稍加修改 </span>
<span>static</span> <span>$tot</span> = 0<span>; 
</span><span>$ds</span> = <span>opendir</span>(<span>$dirname</span><span>); 
</span><span>while</span>(<span>$file</span> = <span>readdir</span>(<span>$ds</span><span>)) { 
</span><span>$path</span> = <span>$dirname</span>.'/'.<span>$file</span><span>; 
</span><span>if</span>(<span>is_dir</span>(<span>$file</span><span>)) { 
</span><span>//</span><span>echo "DIR:".$file."<br>"; </span>
<span>if</span>(<span>$file</span> != "." && <span>$file</span> != ".."<span>) { 
</span><span>$tot</span> += totdir(<span>$file</span><span>); 
} 
} www</span>.jbxue.<span>com
</span><span>else</span><span> { 
</span><span>//</span><span>echo "FILE:".$file . "<br>"; </span>
<span>$tot</span> += <span>filesize</span>(<span>$path</span><span>); 
} 
}

</span><span>//</span><span>返回总计 </span>
<span>return</span> <span>$tot</span><span>; 
}

listdir(</span><span>$dirname</span><span>);

</span><span>echo</span> totdir(<span>$dirname</span>)." bytes"<span>;

</span>?>
登录后复制

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/781159.htmlTechArticle分享一个 PHP遍历目录并返回统计目录大小的方法。 代码: ? php $dirname = "test1" ; // mkdir($dirname);//遍历一层目录 function listdir( $dirname ) { $ds...
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板