【问题标题】:Using an ignore array while listing files RecursiveIterator在列出文件时使用忽略数组 RecursiveIterator
【发布时间】:2012-12-30 19:12:52
【问题描述】:

我正在使用这个函数递归地获取文件列表,它可以工作。

但是,如何修改它以使其忽略$ignore 数组中定义的文件和文件夹?

  function directoryScan($path = 'files', $onlyfiles = true, $fullpath = false)
  {
    $dir = '';
    $ignore = array('.','..','cgi-bin','.DS_STORE','thumb.db','.gitignore', 'folder_to_be_ignored');

    if (isset($path) && is_readable($path))
    {
      $dlist = Array();
      $dir = realpath($path);

      if ($onlyfiles)
      {
        $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
      }
      else
      {
        $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);
      }

      foreach($objects as $entry => $object)
      {
        if (!$fullpath)
        {
          $entry = str_replace($dir, '', $entry);
          $entry = $path . $entry;
        }

        $dlist[] = $entry;
      }

      vdebug($dlist);
      return $dlist;
    }
  }

【问题讨论】:

    标签: php recursion iterator


    【解决方案1】:
      foreach($objects as $entry => $object)
      {
        if (!in_array($entry, $ignore)
        {
          if (!$fullpath)
          {
            $entry = str_replace($dir, '', $entry);
            $entry = $path . $entry;
          }
    
          $dlist[] = $entry;
        }
      }
    

    【讨论】:

    • 还有一件事。如何忽略整个文件夹及其所有文件?
    • 我可能会重新编写它以手动检查目录结构,但作为快速修复,您可以将 this 函数复制到您的代码中,然后将行更改为:@987654323 @.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2016-02-01
    • 2014-12-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多