继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

PHP以树状形式遍历目录结构

喵喵一只汪
关注TA
已关注
手记 315
粉丝 86
获赞 467


<?php//遍历当前目录下所有文件的和目录,并以树装形式显示//1.打开目录句柄,获取句柄资源//2.读取句柄资源,并显示当前和子目录下的(目录和文件名称)function getDirFile($path){        if($file_handler=opendir($path)){            while(false !== ($file=readdir($file_handler))){                                if($file!="." && $file!=".."){                        if(is_dir("$path/$file")){                                               if(substr_count("$path/$file","/")>1){                                $count=str_repeat("&nbsp&nbsp&nbsp&nbsp",substr_count("$path/$file","/"));                                echo $count.$file;                                                               }else{                                echo $file;                                                               }                            echo "<br/>";                            getDirFile("$path/$file");                                                       }else{                                                          if(substr_count("$path/$file","/")>1){                                $count=str_repeat("&nbsp&nbsp&nbsp&nbsp",substr_count("$path/$file","/"));                                echo $count.$file;                                                               }else{                                echo $file;                                                               }                            echo "<br/>";                                                                                      }                                        }            }        }}$path="D:\software\wamp\www\messageboards";getDirFile($path);?>


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP

热门评论

学习了,谢谢你啦! 在网上学习一些东西,代码中常常有bug,而你的,一次性运行成功,谢谢你!

查看全部评论