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;
    }

  • 相关阅读:
    gradle下载安安装教程
    mybatis 一对一association ,一对多collection
    Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): ModuleBuildError:
    API
    VUE 自定组件,注意事项,注意事件
    IDEA设置
    正则表达式
    前端页面适配的rem换算
    webpack打包原理
    vue-cli脚手架工具根目录的babelrc配置文件
  • 原文地址:https://www.cnblogs.com/laowenBlog/p/6531487.html
Copyright © 2011-2022 走看看