rondog Posted December 16, 2009 Share Posted December 16, 2009 Say I have a folder called 'reports' and inside 'reports' I have other folders with files in them..how do I loop through the reports folder to output the folder name and then the files under them? Quote Link to comment https://forums.phpfreaks.com/topic/185406-folder-output/ Share on other sites More sharing options...
cags Posted December 16, 2009 Share Posted December 16, 2009 Using recursion. Here's a simpl(e|ish) script I wrote awhile back for a challenge on another forum. function display_contents_ordered($path) { // vars $dirs = array(); $files = array(); // read directory $content = scandir($path); // loop through contents foreach($content as $file) { // ignore loop up commands if($file != '.' && $file != '..') { // split files and directories if(is_dir($file)) { array_push($dirs, $file); } else { array_push($files, $file); } } } // output directory echo "<li class='dir'>" . $path . "</li>\r\n"; echo "<ul>\r\n"; // loop directories foreach($dirs as $v) { display_contents_ordered($v); } // output files if(count($files) != 0) { foreach($files as $v) { echo "<li class='file'>" . $v . "</li>"; } } else { echo "<li class='no_files'>-- No Files --</li>"; } echo "</ul>\r\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/185406-folder-output/#findComment-978774 Share on other sites More sharing options...
rondog Posted December 16, 2009 Author Share Posted December 16, 2009 That seems to be working for listing the folders inside of $path, but Its not listing the files inside the folders inside $path Quote Link to comment https://forums.phpfreaks.com/topic/185406-folder-output/#findComment-978801 Share on other sites More sharing options...
Daniel0 Posted December 16, 2009 Share Posted December 16, 2009 $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/something')); foreach ($iterator as $item) { if ($iterator->isDot()) continue; echo $item->getPathname() . PHP_EOL; } Quote Link to comment https://forums.phpfreaks.com/topic/185406-folder-output/#findComment-978812 Share on other sites More sharing options...
rondog Posted December 17, 2009 Author Share Posted December 17, 2009 $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/something')); foreach ($iterator as $item) { if ($iterator->isDot()) continue; echo $item->getPathname() . PHP_EOL; } Ahh thats perfect..one question..what is PHP_EOL ? Quote Link to comment https://forums.phpfreaks.com/topic/185406-folder-output/#findComment-978843 Share on other sites More sharing options...
Daniel0 Posted December 17, 2009 Share Posted December 17, 2009 PHP_EOL is just a built-in constant holding the end of line character sequence for the operating system PHP is currently running on. You can replace it with anything else of course. Also, I fixed up your bbcode quote in your post. Quote Link to comment https://forums.phpfreaks.com/topic/185406-folder-output/#findComment-978844 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.