Getting All the Files Recursively From a Directory

Problem

You want to fetch all the files recursively from your file system.

Solution

Use the File::allFiles() method.

$files = File::allFiles($directory);
foreach ($files as $file)
{
    echo (string)$file, "\n";
}

Discussion

The method returns an array of SplFileInfo objects.

Specifically, it returns a Symfony\Component\Finder\SplFileInfo object which is derived from the basic PHP SplFileInfo class to add relative paths. Each object returned will represent a single file (no directories are returned).

If the directory doesn't exist, an InvalidArgumentException will be thrown.

comments powered by Disqus