php - Find directory name and image names in it -


i using short script list images within specific folder

$dirname = "images/gallery/project 1/"; $images = glob($dirname."*.jpg"); foreach($images $imagesm) { print "$imagesm"; } 

however, in folder gallery have 3 folders (project 1, project 2, project 3) , want make script go through each folder automatically , print images them.

for example:

project 1 - image 1 - image 2 - image 3

project 2 - image 1 - image 2 - image 3

project 3 - image 1 - image 2 - image 3

i know can copy script each folder, want happen automatically might add folder project 4 in future

i guess need loop through folders, no idea how it. tried way, didnt work either

$dir = "images/gallery/"; $cdir = scandir($dir);      foreach ($cdir $dirname)      { $newdir="images/gallery/$dirname"; $images = glob($newdir."*.jpg"); foreach($images $imagesm) { print "$imagesm"; } } 

here final working code if interested

$mydir = 'images/gallery';  $subdirs = array_filter(glob($mydir . '/*'), 'is_dir'); foreach($subdirs $dir){   echo '<b>'.basename($dir).'</b><br>';   $images = glob($dir . '/*.jpg');   foreach($images $image)   {     echo basename($image).'<br/>';   } } 

you can make $numfolders variable number of folders have, use a for loop:

$numfolders = 3; //change reflect number of folders  for($i = 1; $i <= $numfolders; $i++){     $dirname = "images/gallery/project $i/";     $images = glob($dirname."*.jpg");     foreach($images $imagesm) {         print "$imagesm";     } } 

this loop through code, going through each project $i directory until reaches limit of $numfolders.


edit

for unknown folder names:

$root = "images/gallery/"; //your root folder  $folders = scandir($root); //get file/folder names  foreach($folders $f){      //check $f valid directory     if($f != "." && $f != ".." && is_dir($f)){          //get files in directory $f         $images = glob($root.$f."*.jpg");          //print images         foreach($images $i){             print $i;         }     } } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -