Posted By

svenito on 09/29/09


Tagged

array recurse subdirectory


Versions (?)

Get all subdirectories of a given directory


 / Published in: PHP
 

Given a start path will return an array of all subdirectories excluding files

  1. function getAllSubdirectories($base) {
  2. $dir_array = array();
  3. if (!is_dir($base)) {
  4. return $dir_array;
  5. }
  6.  
  7. if ($dh = opendir($base)) {
  8. while (($file=readdir($dh)) !== false) {
  9. if ($file == '.' || $file == '..') continue;
  10.  
  11. if (is_dir($base.'/'.$file)) {
  12. $dir_array[] = $file;
  13. } else {
  14. array_merge($dir_array, rendertask::getAllSubdirectories($base.'/'.$file));
  15. }
  16. }
  17. closedir($dh);
  18. return $dir_array;
  19. }
  20. }

Report this snippet  

You need to login to post a comment.