// 递归的方式实现
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;
}

 

posted on 2017-03-10 16:50  啥也不会的程序猿  阅读(440)  评论(0编辑  收藏  举报