Warning: You are browsing the documentation for Symfony 4.1, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

The Finder Component

The Finder Component

The Finder component finds files and directories via an intuitive fluent interface.


$ composer require symfony/finder

Alternatively, you can clone the https://github.com/symfony/finder repository.


If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.


The Symfony\Component\Finder\Finder class finds files and/or directories:

use Symfony\Component\Finder\Finder;

$finder = new Finder();

foreach ($finder as $file) {
    // dumps the absolute path

    // dumps the relative path to the file, omitting the filename

    // dumps the relative path to the file

The $file is an instance of Symfony\Component\Finder\SplFileInfo which extends PHP’s own SplFileInfo to provide methods to work with relative paths.

The above code prints the names of all the files in the current directory recursively. The Finder class uses a fluent interface, so all methods return the Finder instance.


A Finder instance is a PHP IteratorAggregate. So, in addition to iterating over the Finder with foreach, you can also convert it to an array with the iterator_to_array function, or get the number of items with iterator_count.


The Finder object doesn’t reset its internal state automatically. This means that you need to create a new instance if you do not want get mixed results.


When searching through multiple locations passed to the in() method, a separate iterator is created internally for every location. This means we have multiple result sets aggregated into one. Since iterator_to_array uses keys of result sets by default, when converting to an array, some keys might be duplicated and their values overwritten. This can be avoided by passing false as a second parameter to iterator_to_array.


There are lots of ways to filter and sort your results. You can also use the hasResults() method to check if there’s any file or directory matching the search criteria.


The location is the only mandatory criteria. It tells the finder which directory to use for the search:


Search in several locations by chaining calls to in():

// search inside *both* directories
$finder->in([__DIR__, '/elsewhere']);

// same as above

Use wildcard characters to search in the directories matching a pattern:


Each pattern has to resolve to at least one directory path.

Exclude directories from matching with the exclude() method:

// directories passed as argument must be relative to the ones defined with the in() method

It’s also possible to ignore directories that you don’t have permission to read:


As the Finder uses PHP iterators, you can pass any URL with a supported protocol:

// always add a trailing slash when looking for in the FTP root dir

// you can also look for in a FTP directory

And it also works with user-defined streams:

use Symfony\Component\Finder\Finder;

$s3 = new \Zend_Service_Amazon_S3($key, $secret);

$finder = new Finder();
$finder->name('photos*')->size('< 100K')->date('since 1 hour ago');
foreach ($finder->in('s3://bucket-name') as $file) {
    // ... do something with the file


Read the Streams documentation to learn how to create your own streams.

Files or Directories

By default, the Finder returns files and directories; but the files() and directories() methods control that:



If you want to follow links, use the followLinks() method:


By default, the iterator ignores popular VCS files. This can be changed with the ignoreVCS() method:



Sort the result by name or by type (directories first, then files):




By default, the sortByName() method uses the strcmp PHP function (e.g. file1.txt, file10.txt, file2.txt). Pass true as its argument to use PHP’s natural sort order algorithm instead (e.g. file1.txt, file2.txt, file10.txt).

New in version 4.2: The option to use the natural sort order was introduced in Symfony 4.2.

Sort the files and directories by the last accessed, changed or modified time:




You can also define your own sorting algorithm with sort() method:

$finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) {
    return strcmp($a->getRealPath(), $b->getRealPath());


Notice that the sort* methods need to get all matching elements to do their jobs. For large iterators, it is slow.

File Name

Restrict files by name with the name() method:


The name() method accepts globs, strings, or regexes:


The notName() method excludes files matching a pattern:


File Contents

Restrict files by contents with the contains() method:

$finder->files()->contains('lorem ipsum');

The contains() method accepts strings or regexes:


The notContains() method excludes files containing given pattern:

$finder->files()->notContains('dolor sit amet');


Restrict files and directories by path with the path() method:

// matches files that contain "data" anywhere in their paths (files or directories)
// for example this will match data/*.xml and data.xml if they exist

On all platforms slash (i.e. /) should be used as the directory separator.

The path() method accepts a string or a regular expression:


Internally, strings are converted into regular expressions by escaping slashes and adding delimiters:

dirname    ===>    /dirname/
a/b/c      ===>    /a\/b\/c/

The notPath() method excludes files by path:


File Size

Restrict files by size with the size() method:

$finder->files()->size('< 1.5K');

Restrict by a size range by chaining calls:

$finder->files()->size('>= 1K')->size('<= 2K');

The comparison operator can be any of the following: >, >=, <, <=, ==, !=.

The target value may use magnitudes of kilobytes (k, ki), megabytes (m, mi), or gigabytes (g, gi). Those suffixed with an i use the appropriate 2**n version in accordance with the IEC standard.

File Date

Restrict files by last modified dates with the date() method:

$finder->date('since yesterday');

The comparison operator can be any of the following: >, >=, <, <=, ==. You can also use since or after as an alias for >, and until or before as an alias for <.

The target value can be any date supported by the strtotime function.

Directory Depth

By default, the Finder recursively traverse directories. Restrict the depth of traversing with depth():

$finder->depth('== 0');
$finder->depth('< 3');

Custom Filtering

To restrict the matching file with your own strategy, use filter():

$filter = function (\SplFileInfo $file)
    if (strlen($file) > 10) {
        return false;


The filter() method takes a Closure as an argument. For each matching file, it is called with the file as a Symfony\Component\Finder\SplFileInfo instance. The file is excluded from the result set if the Closure returns false.

Reading Contents of Returned Files

The contents of returned files can be read with getContents():

use Symfony\Component\Finder\Finder;

$finder = new Finder();

foreach ($finder as $file) {
    $contents = $file->getContents();

    // ...

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.