手记

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


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


0人推荐
随时随地看视频
慕课网APP

热门评论

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

查看全部评论