Using PHP to traverse the directory, only to traverse the first layer, if the production of online file Manager is very useful, different directories only add a hyperlink on the line, and then give the method to pass parameters on the line, the class to traverse the directory is as follows:
1 classergodic{2 Public function dir($path){3 //traverse the first level of the directory4 $handle=Opendir($path);//Open Directory5 while(($item=Readdir($handle))!==false) {6 //Looping through Directories7 if($item! = '. ' &&$item! = ' ... '){8 if(Is_file($path. "/" .$item )) {9 $arr[' File '] [] =$item;Ten } One if(Is_dir($path. "/" .$item )) { A $arr[' dir '] [] =$item; - } - } the } - Closedir($handle); - return $arr; - } +}
The Dir () method in this class returns an array of $arr, which includes all of the file names and directory names we need, and is simple to use:
1 $dir=Newergodic ();2 $path= "Resource";3 $arr=$dir-dir($path);4 Echo"File list: <br/>";5 if($arr[' File ']){6 foreach($arr[' File '] as $key=$value) {7 Echo($key+1). ' '.$value.‘ <br/> ';8 }9 }Ten Echo"Directory list: <br/>"; One if($arr[' dir ']){ A foreach($arr[' dir '] as $key=$value) { - Echo($key+1). ' '.$value.‘ <br/> '; - } the}
So that we can print out the directory traversal results we specified, randomly built a few files, put in the directory resource, the test image is as follows:
Traversing directories with PHP implementations