Home  >  Article  >  Backend Development  >  A function that traverses file directories. I have two questions. Could you please give me some advice?

A function that traverses file directories. I have two questions. Could you please give me some advice?

WBOY
WBOYOriginal
2016-07-25 08:49:491023browse
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='')?
  1. //Traverse files (file list)
  2. static function scan($dir,$ext_name=true){
  3. $dir_tree=array();
  4. static $file_list=null;
  5. static $the_file_list=null;
  6. foreach (scandir($dir) as $file) {
  7. $file_location=trim($dir,'/').'/'.$file;
  8. if (is_dir($file_location) && $file!=" ." && $file!="..") {
  9. array_push($dir_tree,self::scan($file_location,$ext_name));
  10. } else {
  11. if (($file!='.' && $ file!='..') || !is_dir($file)) {
  12. if ($ext_name===true) {
  13. $file_list[]=$file_location;
  14. }
  15. if ($ext_name && $ext_name= =trim(strrchr($file,'.'))) {
  16. $the_file_list[]=$file_location;
  17. }
  18. $tmp=explode('/',$dir);
  19. array_push($dir_tree,'< strong>'.end($tmp).' '.$file);
  20. }
  21. }
  22. }
  23. $result=($ext_name===true) ? $file_list : $the_file_list;
  24. return $ext_name ? $result : $dir_tree;
  25. }
Copy code


Statement:
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
Previous article:WeChat message sendingNext article:WeChat message sending