Heim >Backend-Entwicklung >PHP-Tutorial >一个遍历文件目录的function,有两个问题请指教?

一个遍历文件目录的function,有两个问题请指教?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-25 08:49:491090Durchsuche
问题一、static $file_list 的static是为递归叠加使用,如何避免外面两次以上调用,后边的结果里叠加前边调用的结果?
问题二、形参$ext_name是否分成两个参数($mode=true,$ext_name='')比较合理?
  1. //遍历文件(文件列表)
  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,''.end($tmp).' '.$file);
  20. }
  21. }
  22. }
  23. $result=($ext_name===true) ? $file_list : $the_file_list;
  24. return $ext_name ? $result : $dir_tree;
  25. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:微信信息发送 Nächster Artikel:全能的字节单位转换函数