$dirs = array(); $file = ''; $handle = ''; //this will probably have to become a function with the way it's going.. if( $handle = opendir('images/thumbs') ) { $i = 0; while(( $file = readdir( $handle )) !== false ) { if( strpos( $file, '.' ) ) { //makes sure you get a .jpg instead of a directory $file = htmlspecialchars( $file ); echo( "" . $file . "\n" ); } else { $dirs[$i] = $file; } $i++; } closedir( $handle ); $file = ''; $i = 0; //this gets all the folders the initial one didn't and parses out . and .. foreach( $dirs as $dir ) { echo( $dir ); if( $dir != '.' && $dir != '..' ) { //hopefully prevents recursion.... maybe think about using is_dir()? if( $handle = opendir( 'images/thumbs/' . $dir ) ) { while(( $file = readdir( $handle )) !== false ) { if( strpos( $file, '.' ) ) { $file = htmlspecialchars( $file ); /* echo( $dir . $file ); */ echo( "" . $file . "\n" ); } } } } $i++; } } closedir( $handle ); $dirs = ''; $file = '';