slimboy007 Posted June 21, 2010 Share Posted June 21, 2010 how can i get a list of filename and filepaths with the extension '.png' in the following directory structure, advanced solution should allow further files and directories to be added at any point in the tree: /* - folder - test1.png - test2.png - untitled.txt - subfolder - test3.png - untitled1.txt */ Quote Link to comment Share on other sites More sharing options...
phpchamps Posted June 21, 2010 Share Posted June 21, 2010 Below is the recursive function. Please note it can be memory overhead. this is only to get the filename you can modify it for the filepath also. function getDirectoryTree( $outerDir ){ $dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) ); $dir_array = Array(); foreach( $dirs as $d ){ if( is_dir($outerDir."/".$d) ) $dir_array[ $d ] = getDirectoryTree( $outerDir."/".$d ); else{ $path_info = pathinfo($d); if($path_info['extension'] == 'png'){ $dir_array[ $d ] = $d; } } } return $dir_array; } $dir = getDirectoryTree("C://wamp/www/answers"); echo "<pre>"; print_r($dir); Quote Link to comment Share on other sites More sharing options...
ignace Posted June 21, 2010 Share Posted June 21, 2010 $iterator = new RecursiveDirectoryIterator( new DirectoryIterator( 'path/to/directory' ) ); while ($iterator->valid()) { if ($iterator->isDot()) continue; echo $iterator->key(), "<br>\n"; } Play with it and modify (extend) to suit your needs. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.