+ -
当前位置:首页 → 问答吧 → 第一题

第一题

时间:2008-10-14

来源:互联网

/*        $path: file or directory path
         *        $depth: file depth in the directory
         */
        function read_dir($path, $depth){                       
                $dirs = array();               
               
                //check if this path is a directory
                if(is_dir($path)){                       
                                $handle = opendir($path);
                               
                                while (false !== ($file = readdir($handle))) {                                                               
                                        if($file != "." && $file != ".."){
                                                //space string for show folder hierarchy
                                                $space = "";

                                                for($i=0;$i<$depth;$i++){
                                                        $space .= "&nbsp;";
                                                }
                                               
                                                //put all dirs into array       
                                                if(is_dir($path."/".$file)){                                                                                       
                                                        //read_dir($path."/".$file);                                                       
                                                        $dirs[] = $path."/".$file;
                                                }else{                                                       
                                                        echo $space."&nbsp;".$file."<br>";
                                                }
                                        }
                                }

                                if(!empty($dirs)){
                                        foreach($dirs as $dir){
                                                $folder = substr(strrchr($dir,"/"),1);                                       
                                                echo $space."-".$folder."<br>";
                                                //iterate invoke
                                                read_dir($dir,$depth+1);
                                        }
                                }                       
                }               
        }

        read_dir("c:/test",0);

作者: liumy601   发布时间: 2008-10-14