Jump to content

Grab Images Form Subfolders


murratw

Recommended Posts

I have a script that grabs images from a folder called "images" and displays them. I would like to modify it to also display the images in all of the subfolders. Can someone help me?

 

<?php

ini_set( "display_errors", 0);
//Your folder
$files = glob("images/*.*");
function sortnewestfilesfirst($a, $B) {
   return filemtime($B) - filemtime($a);
}
usort($files, "sortnewestfilesfirst");
$colCnt=0;
echo '<table border="0" style="width:1000px;">';

for ($i=0;$i<60;$i++)
 {
 $colCnt++;
 if ($colCnt==1)
 echo '<tr>';
 echo '<td width="20%" style="font-size:8.5px; font-family:arial">';
 $num = $files[$i];

echo '
 <div class="ImgBorder">
  <div class="clipout">
   <div class="clipin"> <a href="' . $num . '" rel="lightbox[all]"><img class="thumb ImgBorder" src="'.$num.'"> 
</a> </div>
  </div></div>'."  ";

 echo '</td>';
 if ($colCnt==6)
   {
   echo '</tr>';
   $colCnt=0;
   }
 }
echo '</table>';
?>

Link to comment
Share on other sites

you could use the directory iterator class (part of php5+):

 

$path = 'images/'; // whatever your top level folder is
$images = array();

$dir = new DirectoryIterator($path);
// loop through the chosen directory ( and all sub directories within and choose images)
foreach( $dir as $entry ){
if( $entry->isFile() ){
if( preg_match('#^(.+?)(_t)?\.(jpg|gif|png)#i', $entry->getFilename(), $matches) ){ // check it is an image
array_push($images, $entry->getPathName());

 }
}
}

Edited by gristoi
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.