Find All Files in Directory and Subdirectories using PHP

RecursiveDirectoryIterator class

<?php

$rootDir = 'test_dir';

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootDir));

$files = [];

foreach ($it as $file) {
    if ($file->isFile()) {
        $files[] = $file->getPathname();
    }
}

print_r($files);

Leave a Comment

Cancel reply

Your email address will not be published.