Jessica Posted December 19, 2007 Share Posted December 19, 2007 I am using this function to get all of the files in a directory, and also in any subdirectories in that directory. For the subdirectories, it needs to append the subdirectory to the file. Here is what I am using: function dirList($directory, $add=''){ $results = array(); $handler = opendir($directory); while($file = readdir($handler)){ if($file != '.' && $file != '..'){ if(is_dir($directory.$file)){ $dirResults = dirList($directory.$file, $file.'/'); $results = $dirResults+$results; print_r($results); }else{ $results[] = $add.$file; } } } closedir($handler); return $results; } However, my print_r() only shows the results for the current subdirectory - just $dirResults, not $dirResults+$results. There are four subdirectories, after the first subdirectory it should show the contents of both the first and second in results, then first, second and third, etc. The final array returned has the last subdirectory and all of the files in the main folder. Help Please? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted December 19, 2007 Share Posted December 19, 2007 what does this line do? $results = $dirResults+$results; Quote Link to comment Share on other sites More sharing options...
corbin Posted December 19, 2007 Share Posted December 19, 2007 I came up with this one day when I was making a script to delete all of the files in a folder and it's subfolders. function ListFiles($folder) { $out = array(); $han = opendir($folder); $f = (!empty($folder)) ? $folder . '/' : ''; while($it = readdir($han)) { if($it == '.' || $it == '..') continue; if(!is_dir($f.$it)) { $out[] = (strlen($folder) > 0) ? $folder . '/' . $it : $it; continue; } $path = (strlen($folder) > 0) ? $folder . '/' . $it : $it; $out = array_merge($out, ListFiles($path)); } return $out; } $out = ListFiles('folder'); All it does is return an array where the values are folder/file.... Or folder/subfolder/subfolder/file so on.... The only problem is, it doesn't do anything with empty folders since it only keeps up with files, and that might not be the format you want. Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 19, 2007 Author Share Posted December 19, 2007 Appends the results from the dirList of the subdirectory to the current list of results. I changed it to: $results += dirList($directory.$file, $file.'/'); And now it shows the first subdirectory for the first print_r, and for the other three, the first TWO subdirectories only. I am so confused, it has got to be something simple. Corbin: I don't want to use empty folders, just the files, so I'll try yours. Thanks! Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 19, 2007 Author Share Posted December 19, 2007 Sweet, now I get all 5 folders The only problem is it shows the entire directory, so I'm going to figure out some way to remove the main directory and just show the folder on the subdirectories. Thanks! Edit: Here's my version now. I changed the code to my style so I'd be able to read it later function dirList($folder, $add='') { $out = array(); $han = opendir($folder); $f = (!empty($folder)) ? $folder . '/' : ''; while($file = readdir($han)) { if($file != '.' && $file != '..'){ if(!is_dir($f.$file)) { $out[] = $add.$file; continue; } $out = array_merge($out, dirList($folder.$file, $file.'/')); } } return $out; } Works great, I get all my folders and files. I spent an hour on this, should have come here a bit sooner Quote Link to comment Share on other sites More sharing options...
corbin Posted December 19, 2007 Share Posted December 19, 2007 function ListFiles($folder) { $out = array(); $han = opendir($folder); $f = (!empty($folder)) ? $folder . '/' : ''; while($it = readdir($han)) { if($it == '.' || $it == '..') continue; if(!is_dir($f.$it)) { if(!empty($folder)) { $out[] = (strlen($folder) > 0) ? $folder . '/' . $it : $it; } continue; } $path = (strlen($folder) > 0) ? $folder . '/' . $it : $it; $out = array_merge($out, ListFiles($path)); } return $out; } If that was run in /folder/, all of the subdirs of folder would be included, but the files in folder would be ignored ;p. 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.