How to create a tree of files and folders recursively?

The code below creates a list of files and folders. How to create a tree with unlimited nesting?
public static function getFiles($startDir) 
{
 $files = [];
 if($handle = opendir($startDir)) {
 while (false !== ($i = readdir($handle))) {
 if (is_dir($startDir . DIRECTORY_SEPARATOR . $i)) {
 if (!in_array($i, ['.', '..', 'purity'])) {
 $files[$i] = Purity::getFiles($startDir . DIRECTORY_SEPARATOR . $i);
}
 } elseif (!in_array(pathinfo($i, PATHINFO_EXTENSION), ['dat', 'htaccess', 'txt'])) {
 $files[] = $startDir . DIRECTORY_SEPARATOR . $i;
}
}
closedir($handle);
}
 return $files;
 }
June 5th 19 at 21:43
1 answer
June 5th 19 at 21:45
Teach SPL: RecursiveIteratorIterator + RecursiveDirectoryIterator.

Examples / Brief description: https://stackoverflow.com/a/12236744/790304

Find more questions by tags PHP