欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

如何使用PHP简单地进行文件夹递归遍历

最编程 2024-07-21 12:59:03
...
<?php
function  get_files( $dir ) {
     $files  array ();
     if (! is_dir ( $dir )) {
         return  $files ;
     }
     $handle  = opendir( $dir );
     if ( $handle ) {
         while (false !== ( $file  = readdir( $handle ))) {
             if  ( $file  !=  '.'  &&  $file  !=  '..' ) {
                 $filename  $dir  "/"   $file ;
                 if ( is_file ( $filename )) {
                     if (preg_match( '/.*\.php$/' , $filename )){
                         $files [] =  $filename ;
                     }
                 } else  {
                     $files  array_merge ( $files , get_files( $filename ));
                 }
             }
         }    //  end while
         closedir ( $handle );
     }
     return  $files ;
}

推荐阅读