0

我正在使用此函数以递归方式获取文件列表,并且它可以工作。

但是,如何修改它以使其忽略$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;
    }
  }
4

1 回答 1

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

      $dlist[] = $entry;
    }
  }
于 2013-01-16T13:01:46.997 回答