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

PHP traverses directories and returns statistical directory size_PHP tutorial

WBOY
Release: 2016-07-13 10:28:43
Original
852 people have browsed it

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...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template