PHP遍历目录 PHP遍历目录并返回统计目录大小

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

PHP遍历目录 PHP遍历目录并返回统计目录大小

  2021-03-19 我要评论
想了解PHP遍历目录并返回统计目录大小的相关内容吗,在本文为您仔细讲解PHP遍历目录的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:遍历目录,统计目录大小,下面大家一起来学习吧。
复制代码 代码如下:

<?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";

?>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们