Get all subdirectories of a given directory


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.