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

  • 相关阅读:
    EOF输入
    2019春总结作业
    2019春第二次课程设计报告
    2019春第一次课程设计实验报告
    2019第一次作业的项目模块结构介绍
    2019春第十二周作业
    2019春第十一周作业
    2019春第十周作业
    2019春第九周作业
    2019春第八周作业
  • 原文地址:https://www.cnblogs.com/laowenBlog/p/6531487.html
Copyright © 2020-2023  润新知