Search code examples
phphtmlforeachgalleryauto-populate

Get images from multiple folders within /images and place each folders contents in its own div


I am trying to make php do the following.

I have a structure of folders in my root directory. /images/1234, /images/2345, /images/3456 etc. (multiple folders all in images)

I would like to get the contents of each folder and put their contents into their own div, using php and not hard coding as the folders and files will change regularly.

<div><img src="images/1234/1.jpg"><img src="images/1234/2.jpg"><img src="images/1234/3.jpg"></div>
<div><img src="images/2345/1.jpg"><img src="images/2345/2.jpg"><img src="images/2345/3.jpg"></div>
<div><img src="images/3456/1.jpg"><img src="images/3456/2.jpg"><img src="images/3456/3.jpg"></div>

The files/folders will constantly be added so these divs would have to be in some sort of foreach loop for each folder found in the images directory.

Essentially each folder in images folder would be a div, the divs would auto populate as the browser was refreshed if new folder existed.

I can make it work with php to bring out all images and just place on page,r but I need this structure instead. Thanks for any help.


Solution

  • foreach (glob('images/*') as $dir) {
      echo '<div>'; // One div per directory
      foreach (glob($dir."/*.jpg") as $img) {
        echo "<img src='$img'/>";
      }
      echo "</div>\n";
    }