Question 1: The static of static $file_list is used for recursive superposition. How to avoid more than two external calls, and the results of the previous calls are superimposed on the subsequent results? Question 2: Is it reasonable to divide the formal parameter $ext_name into two parameters ($mode=true, $ext_name='')?
- //Traverse files (file list)
- static function scan($dir,$ext_name=true){
-
- $dir_tree=array();
- static $file_list=null;
- static $the_file_list=null;
-
- foreach (scandir($dir) as $file) {
-
- $file_location=trim($dir,'/').'/'.$file;
-
- if (is_dir($file_location) && $file!=" ." && $file!="..") {
- array_push($dir_tree,self::scan($file_location,$ext_name));
- } else {
-
- if (($file!='.' && $ file!='..') || !is_dir($file)) {
- if ($ext_name===true) {
- $file_list[]=$file_location;
- }
-
- if ($ext_name && $ext_name= =trim(strrchr($file,'.'))) {
- $the_file_list[]=$file_location;
- }
-
- $tmp=explode('/',$dir);
- array_push($dir_tree,'< strong>'.end($tmp).' '.$file);
- }
-
- }
-
- }
-
- $result=($ext_name===true) ? $file_list : $the_file_list;
- return $ext_name ? $result : $dir_tree;
- }
Copy code
|