Home  >  Article  >  Backend Development  >  PHP traverses directories and returns statistical directory size_PHP tutorial

PHP traverses directories and returns statistical directory size_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:43777browse

Copy code The code is as follows:

$dirname = "test1";
//mkdir( $dirname);

//Traverse one level of directories
function listdir($dirname) {
$ds = opendir($dirname);
while($file = readdir($ds )) {
$path = $dirname.'/'.$file;
if(is_dir($file)) {
echo "DIR:".$file."
";
if($file != "." && $file != "..") {
listdir($file);
}
}
else {
echo " FILE:".$file . "
";
}
}
}

function totdir($dirname) { //Slightly modify listdir
static $tot = 0;
$ds = opendir($dirname);
while($file = readdir($ds)) {
$path = $dirname.'/'.$file;
if(is_dir($file)) {
//echo "DIR:".$file."
";
if($file != "." && $file != ". .") {
$tot += totdir($file);
}
}
else {
//echo "FILE:".$file . "
" ;
$tot += filesize($path);
}
}

//Return total
return $tot;
}

listdir ($dirname);

echo totdir($dirname)." bytes";

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/781034.htmlTechArticleCopy the code as follows: ?php $dirname = "test1"; //mkdir($dirname); // Traverse one level of directories function listdir($dirname) { $ds = opendir($dirname); while($file = readdir($ds)) { $pat...
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