Such a problem: you need to implement a tree of files and folders on the site. Accordingly, the attached files should be displayed under their parent folder, and files without a folder (located in the root directory) are simply displayed at the bottom of all the others. I have the following code:
function tmlFile($dir){ $ok = scandir($dir, 0); foreach($ok as $k=>$v){ if($v != '.' && $v != '..'){ if(is_dir($dir.$v) == true){ echo 'папка - > <b>'.$dir.$v.'</b><br>'; tmlFile($dir.$v.'/'); } elseif(is_file($dir.$v) == true){ echo 'файл -> '.$dir.$v.'<br>'; } } } } tmlFile($_SERVER['DOCUMENT_ROOT'].'/tpl/templates/default/'); Displays almost everything correctly, at least displays all folders and all files, but what is the error, you will understand from the picture:

As you can see, everything is in a heap. Red is highlighted, respectively, the folder and the files in it. What is not selected is simply the files in the directory (.tpl files). How to clean up?