0

I am using this short script to list images within a specific folder

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

However, in folder gallery I have 3 folders (Project 1, Project 2, Project 3) and I want to make the script go through each folder automatically and print the images from 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 that I can just copy the script for each folder, but want it to happen automatically as I might add a folder Project 4 in the future

I guess I will just need to loop through the folders, but no idea how to do it. Tried to do it this way, but it didnt work either

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

Here is the final working code if someone is interested

$mydir = 'images/gallery'; 
$subdirs = array_filter(glob($mydir . '/*'), 'is_dir');
foreach($subdirs as $dir){
  echo '<b>'.basename($dir).'</b><br>';
  $images = glob($dir . '/*.jpg');
  foreach($images as $image)
  {
    echo basename($image).'<br/>';
  }
}
lStoilov
  • 1,256
  • 3
  • 14
  • 30
  • 5
    Possible duplicate of [Listing all the folders subfolders and files in a directory using php](http://stackoverflow.com/questions/7121479/listing-all-the-folders-subfolders-and-files-in-a-directory-using-php) – Mr. Engineer Feb 08 '16 at 10:33

4 Answers4

1

You can make a $numFolders variable that is the number of folders you have, then use a for loop:

$numFolders = 3; //change this to reflect the number of folders

for($i = 1; $i <= $numFolders; $i++){
    $dirname = "images/gallery/Project $i/";
    $images = glob($dirname."*.jpg");
    foreach($images as $imagesm) {
        print "$imagesm";
    }
}

This will loop through the code, going through each Project $i directory until it reaches the limit of $numFolders.


Edit

For unknown folder names:

$root = "images/gallery/"; //your root folder

$folders = scandir($root); //get all file/folder names

foreach($folders as $f){

    //Check that $f is a valid directory
    if($f != "." && $f != ".." && is_dir($f)){

        //Get all files in the directory $f
        $images = glob($root.$f."*.jpg");

        //Print all images
        foreach($images as $i){
            print $i;
        }
    }
}
Community
  • 1
  • 1
Ben
  • 8,894
  • 7
  • 44
  • 80
  • thats a good idea, but what is I want to call the next folder Cat or Dog. Also, What I am trying to achieve is not to have human interaction if there is a new folder with pictures added. – lStoilov Feb 08 '16 at 10:45
  • @ Ben Pearl Kahan It doesn't work. It shows a blank page (no results) and I have checked that there are no syntax errors. – lStoilov Feb 08 '16 at 11:39
0

Try this

 function dirToArray($dir) { 

    $result = array(); 
    $allowedtypes = array('jpg','png','jpeg');
    $cdir = scandir($dir); 
    foreach ($cdir as $key => $value) 
    { 
       if (!in_array($value,array(".",".."))) 
       { 
          if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
          { 
             $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
          } 
          else if(in_array(pathinfo($value, PATHINFO_EXTENSION),$allowedtypes))
          { 
             $result[] = $value; 
          } 
       } 
    } 

    return $result; 
 } 

$dirname = "images/gallery";
$gallery_arr = array_filter(dirToArray($dirname));
echo '<pre>';print_r($gallery_arr);exit;

source:- php.net

narasimharaosp
  • 533
  • 3
  • 12
0
    <?php
    $images=[];
    $len=2;
        for($i=1;$i<=$len;$i++){
                $dirname = "images/gallery/Project " .$i ."/";
                 $images = glob($dirname."*.jpg");
                 foreach($images as $imagesm) {
                    print "$imagesm";
                } 
    }
    ?>

where $len is a number of folders that you have created.I give here $len=2 because i created **Project 1** and **Project 2**
sanju verma
  • 23
  • 12
0

Okay, I took a bit of a different approach... and simple I believe. Here is the script I did that actually does exactly what I mentioned in the first post

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

Thanks to everyone anyway.

lStoilov
  • 1,256
  • 3
  • 14
  • 30