Get Files recursively in PHP

RecursiveDirectoryIterator to get all files

This implementation also sorts by extension if you switch the $files[] = ... line to the commented one

$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('path/to/folder'));  
$files = [];   
foreach ($rii as $file) {  
    if ($file->isDir()){   
        continue;  
    }  
    //$files[$file->getExtension()][] = $file->getPathname();  
    $files[] = $file->getPathname();   
}  

One-liner function as a variable, with readdir

I need to re-test & confirm this function works properly.
Here's a one-liner that declares nothing in the global scope, thus will not conflict with any functions or classes you have.
The readdir approach could be a problem with significantly nested directories where too many file pointers get opened. scandir would resolve this.

$files =   
(new class{  
    function getFiles($path,$ext='*'){  
        $dh = opendir($path);  
        $files = [];  
        while ($file = readdir($dh)){  
            if ($file=='.'||$file=='..')continue;  
            $fPath = str_replace('//','/',$path.'/'.$file);  
            if (is_dir($fPath)){  
                $subFiles = $this->getFiles($fPath,$ext);  
                $files = array_merge($files,$subFiles);  
            } else if ($ext=='*'  
                ||($fExt=pathinfo($fPath,PATHINFO_EXTENSION))==$ext){  
                    var_dump($fExt);  
                $files[] = $fPath;  
            }  
        }  
        return $files;  
    }  
})->getFiles($dir,$ext);  

And as a single line, provided you declare $dir and $ext

$files = (new class{ function getFiles($path,$ext='*'){ $dh = opendir($path); $files = []; while ($file = readdir($dh)){ if ($file=='.'||$file=='..')continue; $fPath = str_replace('//','/',$path.'/'.$file); if (is_dir($fPath)){ $subFiles = $this->getFiles($fPath,$ext); $files = array_merge($files,$subFiles); } else if ($ext=='*' ||($fExt=pathinfo($fPath,PATHINFO_EXTENSION))==$ext){ var_dump($fExt); $files[] = $fPath; } } return $files; } })->getFiles($dir,$ext);