0

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;
    }
  }
Will Sewell
  • 2,593
  • 2
  • 20
  • 39
YahyaE
  • 1,057
  • 1
  • 9
  • 24

1 Answers1

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

      $dlist[] = $entry;
    }
  }
Will Sewell
  • 2,593
  • 2
  • 20
  • 39
  • One more thing. How can I ignore a whole folder with all its file? – YahyaE Jan 16 '13 at 13:17
  • 1
    I'd probably re-write it to go through the directory structure manually, but as a quick fix, you could copy [this](http://stackoverflow.com/a/2124557/1018290) function into your code, and then change the line to: `if (!contains($entry, $ignore))`. – Will Sewell Jan 18 '13 at 03:37