zoukankan      html  css  js  c++  java
  • PHP 遍历文件夹下的文件以及子文件夹

    // 递归的方式实现
    function my_dir( $dir )
    {
      if ( !is_dir($dir) )
      {
        return 'not dir';die();
      }
      $files = array();
      $dir_list = scandir($dir);
      foreach( $dir_list as $file )
      {
        if( $file!='.' && $file!='..' )
        {
          if( is_dir( $dir.'/'.$file ))
          {
            $files[$file] = my_dir( $dir.'/'.$file );
          }
          else
          {
            $files[] = $file;
          }
        }
      }
      return $files;
    };

    // 队列方式实现

    function my_dir_list( $dir )
    {
      if( !is_dir( $dir) )
      {
        return 'not dir';die();
      }
      $files = array();
      $queue = array( $dir );
      // $data = each( $queue );
      while( $data = each( $queue) )
      {
        $path = $data['value'];
        $handle = opendir( $path );
        if( is_dir($path) && $handle )
        {
          // $file = readdir( $handle );
          while( $file = readdir( $handle) )
          {
            if ( $file == '.' || $file == '..' )
            {
              continue;
            }
            else
            {
              $real_path = $path.'/'.$file;
              $files[] = $real_path;
              if( is_dir($real_path) )
              {
                $queue[] = $real_path;
              }
            }
          }
        }
        closedir($handle);
      }
      return $files;
    }

  • 相关阅读:
    node03- FS内置模块
    node03- CommonJS
    删除当前目录下的所有文件夹和文件
    解决 idea 项目中Error:java: 无效的标记
    Raid0,Raid1,Raid5,Raid10 总结
    Tcpdump命令
    ClassNotFoundException 和 NoClassDefFoundError 区别
    Dart-List里面常用的属性和方法
    CSS实现等分布局的4种方式
    iOS项目添加CocoaPods
  • 原文地址:https://www.cnblogs.com/laowenBlog/p/6531487.html
Copyright © 2011-2022 走看看