A function that traverses file directories. I have two questions. Could you please give me some advice?
Release: 2016-07-25 08:49:49
Original
1057 people have browsed it
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
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31