简体   繁体   中英

Using an ignore array while listing files RecursiveIterator

I am using this function to get a list of files recursively, and it works.

However, how can I modify it so that it ignores files and folders that are defined in the $ignore array?

  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;
    }
  }
  foreach($objects as $entry => $object)
  {
    if (!in_array($entry, $ignore)
    {
      if (!$fullpath)
      {
        $entry = str_replace($dir, '', $entry);
        $entry = $path . $entry;
      }

      $dlist[] = $entry;
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM