JSOW Posted July 22, 2008 Share Posted July 22, 2008 Im trying to build a function to pass over a given Dir and retrive files from given Dir and sub Dirs and then return an array built from the Dir structure. It is intended for a gallery page so that I can then bulid the page from said array. After a day of looking over Variable Variables and what I thought would be a reasonable simple bit of code I am at a loss to see where I am going wrong... code follows... <?php function list_files($parent_dir = '.') { //check vairable is a dir if(!is_dir($parent_dir)) { return false; } else { //create arrays, put passed dir into dir list $file_list = array(); $dir_list = array($parent_dir); //check dir list still has variables and grab last dir from list while( NULL !== ($dir = array_pop($dir_list))) { //open dir if( $dh = opendir($dir)) { //check dir for file and grab file from list while( false !== ($file = readdir($dh))) { //skip unwanted dirs if($file == '.' || $file == '..') { continue; } //set full path including file $file_path = $dir.'/'.$file; //if dir place in dir array if( is_dir($file_path)) { $dir_list[] = $file_path; } //if file place into file list array else { unset($array_path); //pull parent dir off dir string $subdirs = substr($dir,strlen($parent_dir)+1); //break srting into dir pieces $array_dirs = explode("/", $subdirs); //cycle through pieces creating array structure foreach ($array_dirs as $array_dir) { $array_path .="[$array_dir]"; } //add file to file list using new array path $file_list{$array_path}[] = $file; } } //close dir closedir($dh); } } //return value return $file_list; } } ?> which is producing this: Array ( [[meremere 27 dec 07]] => Array ( [0] => DSC00029_JPG.jpg [1] => DSC00030_JPG.jpg [2] => DSC00031_JPG.jpg [3] => DSC00033_JPG.jpg [4] => DSC00043_JPG.jpg [5] => DSC00059_JPG.jpg [6] => DSC00060_JPG.jpg [7] => DSC00061_JPG.jpg [8] => DSC00052_JPG.jpg [9] => DSC00036_JPG.jpg [10] => DSC00023_JPG.jpg [11] => DSC00026_JPG.jpg ) [[meremere 27 dec 07][thumbnails]] => Array ( [0] => 012.jpg [1] => 011.jpg [2] => 010.jpg [3] => 009.jpg [4] => 008.jpg [5] => 007.jpg [6] => 006.jpg [7] => 005.jpg [8] => 004.jpg [9] => 003.jpg [10] => 002.jpg [11] => 001.jpg ) ) Obviously its producing a new array named after my array_path variable, not quite what I was after. I want it to output a series of arrays, parrent and child, like a dir structure, without the extra set of square brackets if you will. Hope this is clear, Hope someone can help. Thanks, Ben. Quote Link to comment Share on other sites More sharing options...
jonsjava Posted July 22, 2008 Share Posted July 22, 2008 I'm looking over the code right now, but just in case I get too busy to get back to it (at work), here's a "cleaned up" version of the code. Indents are easier to follow: <?php function list_files($parent_dir = '.') { //check vairable is a dir if(!is_dir($parent_dir)) { return false; } else { //create arrays, put passed dir into dir list $file_list = array(); $dir_list = array($parent_dir); //check dir list still has variables and grab last dir from list while( NULL !== ($dir = array_pop($dir_list))) { //open dir if( $dh = opendir($dir)) { //check dir for file and grab file from list while( false !== ($file = readdir($dh))) { //skip unwanted dirs if($file == '.' || $file == '..') { continue; } //set full path including file $file_path = $dir.'/'.$file; //if dir place in dir array if( is_dir($file_path)) { $dir_list[] = $file_path; } //if file place into file list array else { unset($array_path); //pull parent dir off dir string $subdirs = substr($dir,strlen($parent_dir)+1); //break srting into dir pieces $array_dirs = explode("/", $subdirs); //cycle through pieces creating array structure $array_path = array(); foreach ($array_dirs as $array_dir) { $array_path[] = "[$array_dir]"; } //add file to file list using new array path $file_list{$array_path}[] = $file; } } //close dir closedir($dh); } } //return value return $file_list; } } ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 22, 2008 Share Posted July 22, 2008 Looks like major overkill to me. Try this: <?php function get_structure($dir){ $structure = array(); foreach(glob($dir.'/*') as $v){ if(is_dir($v)){ $structure[basename($v)] = get_structure($v); }else{ $structure[] = basename($v); } } return $structure; } echo '<pre>'.print_r(get_structure('C:/wamp/www'),1).'</pre>';//note no backslash on end ?> Quote Link to comment Share on other sites More sharing options...
JSOW Posted July 22, 2008 Author Share Posted July 22, 2008 Wow! Thanks... I have so much to learn Off to look up Glob and Basename... Ben. PS. Thanks for the code cleanup too. 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.