grasperstail Posted November 15, 2007 Share Posted November 15, 2007 I can't seem to figure out why the $folders array and the $files array are being overwritten (starting from 0 key) when the function goes into recursion. function photo_directory($start_dir){ $files = array(); $dir_array = array(); $folders = array(); if(is_dir($start_dir)){ $fh = opendir($start_dir); while (false !== ($file = readdir($fh))){ if (strcmp($file, ".")==0 || strcmp($file, "..") == 0) continue; $filepath = $start_dir . "/" . $file; print_r($filepath); echo "</br>"; if(is_dir($filepath)){ $folders[] = $filepath; print_r($folders); echo "</br>"; photo_directory($filepath); }else{ $files[] = $filepath; print_r($files); echo "</br>"; } } closedir($fh); }else{ $dir_array = false; } return $dir_array; }$test = photo_directory($start_dir); Link to comment Share on other sites More sharing options...
Synook Posted November 15, 2007 Share Posted November 15, 2007 Because you haven't declared them as global function photo_directory($start_dir){ global $files; $dir_array = array(); gloabl $folders; if(is_dir($start_dir)){ $fh = opendir($start_dir); while (false !== ($file = readdir($fh))){ if (strcmp($file, ".")==0 || strcmp($file, "..") == 0) continue; $filepath = $start_dir . "/" . $file; print_r($filepath); echo "</br>"; if(is_dir($filepath)){ $folders[] = $filepath; print_r($folders); echo "</br>"; photo_directory($filepath); }else{ $files[] = $filepath; print_r($files); echo "</br>"; } } closedir($fh); }else{ $dir_array = false; } return $dir_array; }$files = array();$folders = array();$test = photo_directory($start_dir); Link to comment Share on other sites More sharing options...
grasperstail Posted November 15, 2007 Author Share Posted November 15, 2007 Thank you kindly sir. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.