I have a folder (root folder) with thousands of sub folders and *.json files inside sub folders. How can I get list of sub folder names inside folder (root folder) using file_scan_directory?


If file_scan_directory usually returns directories then perhaps you can use the regex mask to search for names that don't have a dot (.) in them.

There are very few files in a Drupal installation that don't have an extension, and I imagine there will be none in your folder structure if all it contains is .json files...

file_scan_directory($your_root_folder, '/^[^\.]+$/');

The above should match all items in the directory structure that don't contain a dot, thanks to this answer.

|improve this answer|||||
  • It seems that file_scan_directory() does not return directories, so this won't work. – Adam Franco Aug 26 '15 at 21:02

I'd use RecursiveDirectoryIterator for that.

   $path = '/your/awesome/absolute/path';
   $dir = new RecursiveDirectoryIterator($path);
   $json_files = new RegexIterator($dir, '/\.json$/');
   foreach ($json_files as $file) {
     // Do you stuff here.
     echo "$file";
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.