PHP遍歷目錄并返回統(tǒng)計目錄大小
更新時間:2014年06月09日 11:31:02 作者:
這篇文章主要介紹了PHP遍歷目錄并返回統(tǒng)計目錄大小的方法,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
<?php
$dirname = "test1";
//mkdir($dirname);
//遍歷一層目錄
function listdir($dirname) {
$ds = opendir($dirname);
while($file = readdir($ds)) {
$path = $dirname.'/'.$file;
if(is_dir($file)) {
echo "DIR:".$file."<br>";
if($file != "." && $file != "..") {
listdir($file);
}
}
else {
echo "FILE:".$file . "<br>";
}
}
}
function totdir($dirname) { //對listdir稍加修改
static $tot = 0;
$ds = opendir($dirname);
while($file = readdir($ds)) {
$path = $dirname.'/'.$file;
if(is_dir($file)) {
//echo "DIR:".$file."<br>";
if($file != "." && $file != "..") {
$tot += totdir($file);
}
}
else {
//echo "FILE:".$file . "<br>";
$tot += filesize($path);
}
}
//返回總計
return $tot;
}
listdir($dirname);
echo totdir($dirname)." bytes";
?>
相關(guān)文章
php 數(shù)據(jù)庫字段復(fù)用的基本原理與示例
php 數(shù)據(jù)庫字段復(fù)用的基本原理與示例,需要的朋友可以參考下。2011-07-07
php根據(jù)一個給定范圍和步進生成數(shù)組的方法
這篇文章主要介紹了php根據(jù)一個給定范圍和步進生成數(shù)組的方法,涉及php針對數(shù)組的遍歷技巧,需要的朋友可以參考下2015-06-06

