largan February 2016

Find directory name and image names in it

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/>';
  }
}

Answers


Ben February 2016

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;
        }
    }
}


narasimharaosp February 2016

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


sanju verma February 2016

    <?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**


largan February 2016

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.

Post Status

Asked in February 2016
Viewed 3,200 times
Voted 12
Answered 4 times

Search




Leave an answer