murratw Posted November 6, 2012 Share Posted November 6, 2012 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 https://forums.phpfreaks.com/topic/270361-grab-images-form-subfolders/ Share on other sites More sharing options...
gristoi Posted November 6, 2012 Share Posted November 6, 2012 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()); } } } Link to comment https://forums.phpfreaks.com/topic/270361-grab-images-form-subfolders/#findComment-1390532 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.