Skip to content

FileExtensionFilterIterator

github-actions edited this page Mar 31, 2026 · 3 revisions

A filter iterator that only accepts files matching specified extensions.

This iterator is designed to traverse a directory and filter files by their extension. It supports both FilesystemIterator and RecursiveIterator.

Usage Example:


Methods

__construct

Initializes the FileExtensionFilterIterator.

public __construct(\FilesystemIterator|\RecursiveDirectoryIterator $iterator, string $extensions): mixed

Parameters:

Parameter Type Description
$iterator \FilesystemIterator|\RecursiveDirectoryIterator the directory iterator to wrap
$extensions string The allowed file extensions.

accept

Determines whether the current file should be accepted.

public accept(): bool
  • Directories are accepted only if the underlying iterator is recursive.
  • Files are accepted only if their extension matches the allowed list.

Return Value:

true if the file is accepted, false otherwise


Inherited methods

count

Counts the number of elements exposed by the inner iterator.

public count(): int

If the inner iterator implements

  • See: \Countable, this method SHALL return the value provided by that implementation. Otherwise, it MUST count elements by iterating over the iterator. If the inner iterator is not cloneable, this method SHALL wrap the current object in an

  • See: \IteratorIterator instance and count through that wrapper to avoid performing an invalid clone operation. If the inner iterator is cloneable, this method SHOULD count over a clone so that the original iterator state is preserved as much as possible.

Return Value:

the total number of elements available from the inner iterator


Clone this wiki locally