merc123 Posted April 10, 2008 Share Posted April 10, 2008 I got a directory recurssion snippet off the net and need it to do something that I can't figure out. It works correctly, but appears to make 2 seperate arrays. Any way I can get them to be combined because I want to pick a random number and output a specific file. It is looking in the directory ../gallery which also contains a subdirectory. For each subdirectory it's creating a seperate array. Any way to combine them or a different way to do this? function getDirectory( $folder = '.', $level = 0 ){ // Space seperated list of extensions, you probably won't have to change this. $exts = 'gif jpeg jpg png'; $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts $files = array(); $i = -1; // Initialize some variables if ('' == $folder) $folder = './'; $handle = opendir($folder); $exts = explode(' ', $exts); while (false !== ($file = readdir($handle))) { if( !in_array( $file, $ignore ) ){ if( is_dir( "$folder/$file" ) ){ // Its a directory, so we need to keep reading down... getDirectory( "$folder/$file", ($level+1) ); // Re-call this same function but on a new directory. This is what makes function recursive. } else { //If not a directory... foreach($exts as $ext) { // for each extension check the extension if (preg_match('/\.'.$ext.'$/i', $file, $test) && !eregi("thumb_",$file)) { // faster than ereg, case insensitive $files[] = $folder ."/". $file; // it's good ++$i; } } } } } echo "<pre>"; print_r($files_list); echo "</pre>"; closedir($handle); // We're not using it anymore } getDirectory("../gallery"); Displays: Array ( [0] => ../gallery/0/40_23_03_08_8_26_14.JPG [1] => ../gallery/0/40_23_03_08_8_27_18.JPG [2] => ../gallery/0/40_23_03_08_8_28_52.JPG ) Array ( [0] => ../gallery/40_01_03_08_6_55_14.JPG [1] => ../gallery/40_01_03_08_6_55_44.JPG [2] => ../gallery/40_01_03_08_6_56_24.JPG [3] => ../gallery/40_01_03_08_6_57_01.JPG [4] => ../gallery/40_01_03_08_6_57_36.JPG ) Link to comment https://forums.phpfreaks.com/topic/100526-stumped-by-recurssion-and-array/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.