Library / SDK
sebastianbergmann/php-file-iterator avatar
sebastianbergmann/php-file-iterator

php-file-iterator: A Narrow FilterIterator for PHP File Selection

FilterIterator implementation that filters files based on a list of suffixes, prefixes, and other exclusion criteria.

7,476 stars47 forksPHPBSD-3-Clause

At a glance

What is it?
Sebastian Bergmann's php-file-iterator is a small PHP library that filters files by suffix, prefix, and other criteria, built for developers who need precise control over file iteration without pulling in a larger filesystem abstraction.
Who is it for?
Adopt php-file-iterator if you are a PHP developer who needs a lightweight, dependency-light way to filter files during iteration, especially for build tools or test runners. Do not use it if you require recursive directory walking, complex pattern matching, or a full virtual filesystem.
Can I use it commercially?
Yes. BSD-3-Clause is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 1 day ago.
What is it written in?
Mainly PHP, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on September 15, 2026, and from our analysis. They are not legal advice.

DEEP OPEN-SOURCE ANALYSIS

What This Library Solves and Who It Serves

The problem is simple: when you iterate over a directory in PHP, you often want only files that match certain suffixes or prefixes, or you want to exclude paths that meet specific criteria. The standard DirectoryIterator gives you everything, and writing a custom FilterIterator for each project leads to repetition and subtle bugs. php-file-iterator packages that logic into a single, reusable class. It is built for PHP developers who are already using Composer and who need a focused tool for tasks like collecting source files for analysis, assembling test suites, or processing uploaded files. It is not a general-purpose file manipulation library. It does not copy, move, or read file contents. Its only job is to decide which files pass a filter, and it does that with a narrow, predictable API.

The Mechanism: A FilterIterator with Configurable Criteria

The core of the library is a FilterIterator implementation, as the description states. It filters files based on a list of suffixes, prefixes, and other exclusion criteria. The exact constructor parameters and method signatures are not shown in the README, but the description gives the essential behaviour: you provide a list of suffixes to accept, a list of prefixes to accept, and then exclusion criteria that override those positive matches. The mechanism is straightforward: when the iterator advances, it checks each file against the configured lists. A file passes if it matches at least one suffix and at least one prefix, unless it matches an exclusion rule. The filter operates on file names, not paths, which means it does not handle directory recursion on its own. It works with whatever iterator you feed it, so you can wrap a RecursiveDirectoryIterator if you need to walk subdirectories, but that composition is your responsibility. The library's value is in the consistent, tested logic for these matching rules, which saves you from reimplementing edge cases like case sensitivity or empty lists.

Installation and Basic Usage from the README

The README gives exactly one installation path: Composer. You run `composer require phpunit/php-file-iterator` to add it as a runtime dependency, or `composer require --dev phpunit/php-file-iterator` if you only need it during development, for example to run your project's test suite. The package name is phpunit/php-file-iterator, which reflects its origin within the PHPUnit ecosystem. The README does not show a code example, so the exact way to instantiate the filter is not documented in the material. What is clear is that after installation, you would use it in a PHP script where you create an iterator over a directory and then apply the filter. The lack of a usage snippet is a gap, but the class name and description suggest a constructor that takes an iterator and the filtering parameters. For a developer familiar with PHP's FilterIterator, the integration is intuitive: you wrap an existing iterator, and the filtering happens transparently as you loop.

A Genuine Limitation: No Recursion and No Pattern Matching

The most obvious limitation is that this library does not recursively scan directories on its own. It is a FilterIterator, which means it filters the items that the underlying iterator produces. If you pass it a plain DirectoryIterator, it only sees the top-level files. To get files from subdirectories, you must wrap a RecursiveDirectoryIterator and then apply the filter, but the library does not provide that wrapper. This is a deliberate design choice for simplicity, but it means you, the developer, must understand iterator composition. A second limitation is that the filtering is based on suffixes and prefixes, not on regular expressions or glob patterns. If you need to match files like `*.log` but exclude `access.log`, you can do that with a suffix and an exclusion, but if you need a pattern like `file[0-9].txt`, you are out of luck. The library is the wrong tool for complex pattern matching, and you would be better served by a more general file-finding library or by writing a custom callback.

An Alternative: Symfony Finder's Different Approach

A real alternative is Symfony Finder, a component that provides a fluent interface for finding files and directories. The difference in approach is significant. Symfony Finder is not a FilterIterator; it is a full-fledged file locator that handles recursive traversal, multiple location roots, and a rich set of criteria such as name patterns, file size, modification time, and content. You can chain methods like `->files()->name('*.php')->notPath('vendor')` to build a query. In contrast, php-file-iterator is a minimal filter that you must integrate into your own iteration logic. Symfony Finder gives you a ready-made iterator that you can loop over directly, and it handles the recursion for you. The cost is a larger dependency with more overhead. If you only need to filter a flat list of files by suffix and prefix, php-file-iterator is lighter and more focused. If you need to search a directory tree with multiple conditions, Symfony Finder saves you from assembling iterators manually. The choice comes down to whether you want a small, precise tool or a comprehensive one.

Maintenance, Release Cadence, and Licence Considerations

The repository is active, with recent releases in August 2026. The 7.0.2 release came on 2026-08-25, and 6.0.2 was released the same day, indicating that both major versions receive patches. The project is not archived, and the default branch is main. The licence is BSD-3-Clause, which is permissive and allows use in commercial and open-source projects, including proprietary code, as long as the copyright notice and disclaimer are preserved. The maintenance cost for you is low: the library is small, so upgrades are unlikely to introduce breaking changes unless you jump a major version. However, the README does not document a changelog or upgrade guide, so you must check the release notes on GitHub for each version. The dependency on Composer is standard for PHP, and the package name phpunit/php-file-iterator suggests it is tightly coupled to the PHPUnit ecosystem, so you can expect it to be maintained as long as PHPUnit is active. The dual release line (6.x and 7.x) suggests a commitment to supporting multiple PHP versions, but the exact version requirements are not stated in the material.

Editorial conclusion

Adopt php-file-iterator if you are a PHP developer who needs a lightweight, dependency-light way to filter files during iteration, especially for build tools or test runners. Do not use it if you require recursive directory walking, complex pattern matching, or a full virtual filesystem. Before integrating, verify that the FilterIterator semantics match your iteration order and that the suffix/prefix matching handles case sensitivity as you expect. The library is stable and maintained, but its scope is deliberately minimal, so confirm it meets your exact filtering needs rather than assuming it will grow features.

Official sources

  1. Official README
  2. Project repository
  3. Release notes
Community notes

Community notes