PHP Recursive Directory Iterator - ignore certain files.

Thanks to @Sven and the PHP Docs:

$iterator = new RecursiveDirectoryIterator($directory);
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);

$filter = new MyRecursiveFilterIterator($iterator);

$all_files  = new RecursiveIteratorIterator($filter,RecursiveIteratorIterator::SELF_FIRST);

Then extended RecursiveFilterIterator

class MyRecursiveFilterIterator extends RecursiveFilterIterator {

    public static $FILTERS = array(
        '__MACOSX',
    );

    public function accept() {
        return !in_array(
            $this->current()->getFilename(),
            self::$FILTERS,
            true
        );
    }

}

As per the comment In the PHP Manual

Tags:

Php

Iterator