php - RecursiveDirectoryIterator to echo list separated by folders -


is there way recursivedirectoryiterator echo files in subfolders separately based on folder , not together?

here example. have folder (event), has multiple subfolders (logo, people, bands). subfolder names vary events, can't set inside these three, need "wildcard" option. when use recursivedirectoryiterator echo out images these folders, works, separate these based on subfolder, echoes out folder name , images within below , repeats next folder , on.

right use this:

<?php  $directory = "path/to/mainfolder/"; foreach (new recursiveiteratoriterator(new  recursivedirectoryiterator($directory,recursivedirectoryiterator::skip_dots)) $filename) {   echo '<img src="'.$filename.'">'; } ?> 

so, how make echo like:
logo
image, image, image
people
image, image, image
...

thanks in advance useful tips , ideas.

for me, code more readable when put objects variables descriptive names pass on when instantiating other classes. i've used regexiterator() on recursivefilteriterator() filter image file extensions (didn't want extending recursivefilteriterator() class example). rest of code simple iterating, extracting strings , page breaking.
note: there no error handling, best add try/catch manage exceptions

<?php $directory = 'path/to/mainfolder/';  $objrecursivedirectoryiterator = new recursivedirectoryiterator($directory, recursivedirectoryiterator::skip_dots); $objrecursiveiteratoriterator = new recursiveiteratoriterator($objrecursivedirectoryiterator); // use regexiterator() grab image file extensions $objregexiterator = new regexiterator($objrecursiveiteratoriterator, "~^.+\.(bmp|gif|jpg|jpeg|img)$~i", recursiveregexiterator::get_match);  $lastpath = ''; // iterate through results foreach ($objregexiterator $arrmatches) {     $filename = $arrmatches[0];     $pos = strrpos($filename, directory_separator); // find position of last directory_separator     $path = substr($filename, 0, $pos); // path before     $file = substr($filename, $pos + 1); // file after     $mydir = substr($path, strrpos($path, directory_separator) + 1); // directory file sits in     if ($lastpath !== $path) { // path same last         // display page break , header         if ($lastpath !== '') {             echo "<br />\n";             echo "<br />\n";         }         echo $mydir ."<br />\n";         $lastpath = $path;     }     echo $file . " "; } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -