使用PHP怎么对目录进行遍历

发布时间:2021-01-06 16:49:01 作者:Leah
来源:亿速云 阅读:148

这篇文章给大家介绍使用PHP怎么对目录进行遍历,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

复制代码 代码如下:


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

?>

关于使用PHP怎么对目录进行遍历就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 使用jquery怎么对hash进行遍历
  2. 使用php怎么对一维数组进行循环遍历

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

遍历 php

上一篇:利用php怎么对文章的排行进行统计

下一篇:使用php怎么统计问卷调查结果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》