0

I have the following directory structure:

MainFolder
    Folder1
        Folder1-1
             Folder1-1-1  
             Folder1-1-2
             Folder1-1-3    
        Folder1-2
             Folder1-2-1  
             Folder1-2-2
             Folder1-2-3  
    Folder2
        Folder2-1
             Folder2-1-1  
             Folder2-1-2
             Folder2-1-3  
        Folder2-2
             Folder2-2-1  
             Folder2-2-2
             Folder2-2-3

I'm trying to create 3 arrays

1 - array of all subfolders of MainFolder (Folder1, Folder2..etc)

2 - array of subfolders inside of Folder1, Folder2, etc (e.g:Folder1-1...folder2-1...)

3 - array of subfolders inside Folder1-1..., Folder1-2..., etc

this way I can only filter the subdirectories of the current directory:

//path to directory to scan
$directory = "MainFolder/";

//get all files in specified directory
$files = glob($directory . "*");

//print each file name
foreach($files as $file)
{
 //check to see if the file is a folder/directory
 if(is_dir($file))
 {
  echo $file;
 }
}

but how do I, for glob to filter the current directory and automatically group into array as I showed in the example?

I already saw that RecursiveDirectoryIterator exists but I did not understand how to put it in different arrays

Gislef
  • 1,555
  • 3
  • 14
  • 37
  • 3
    Possible duplicate of [List all the files and folders in a Directory with PHP recursive function](https://stackoverflow.com/questions/24783862/list-all-the-files-and-folders-in-a-directory-with-php-recursive-function) – Madhur Bhaiya Sep 24 '18 at 18:19
  • @MadhurBhaiya Thanks, I already saw this question, but I could not create the separate arrays – Gislef Sep 24 '18 at 18:20
  • http://php.net/manual/en/function.glob.php there's a comment by sharshun that may interest you. – Jhecht Sep 24 '18 at 18:23
  • @Jhecht thanks, but in that case it does a fully sequential array of directories and subdirectories, it does not separate the directories into different arrays, if there is another array is a sequential file found in folders and subfolders – Gislef Sep 24 '18 at 18:40
  • Do you need just 3 arrays, or do you want to keep going as far as possible? – Barmar Sep 24 '18 at 19:21
  • @Barmar just 3 arrays – Gislef Sep 24 '18 at 19:25

2 Answers2

2

You have a fixed low depth, so you don't really need recursive imho.

You can use wildcard * to mark different levels and GLOB_ONLYDIR to retrieve folders only :

$level1 = glob('MainFolder/*', GLOB_ONLYDIR);
$level2 = glob('MainFolder/*/*', GLOB_ONLYDIR);
$level3 = glob('MainFolder/*/*/*', GLOB_ONLYDIR);

If you want to store the last folder instead of full path you can use array_map() and basename() :

$level1 = array_map('basename', glob('MainFolder/*', GLOB_ONLYDIR));
...
JCH77
  • 1,125
  • 13
  • 13
1

Put your code into a function so you can call it for each directory at a particular level.

function all_subdirectories($directory) {
    $files = glob("$directory/*");
    return array_filter($files, 'is_dir');
}

Here's code that will create a 2-dimensional array with every level.

$all_levels = array();
$dirs = array("MainFolder");
while (!empty($dirs)) {
    $next_level = array();
    foreach ($dirs as $dir) {
        $next_level += all_subdirectories($dir);
    }
    if (!empty($next_level)) {
        $all_levels[] = array_map('basename', $next_level);
    }
    $dirs = $next_level;
}

print_r($all_levels);

For just 3 levels it's simpler:

$level1 = all_subdirectories("MainFolder");
$level2 = all_subdirectories("MainFolder/*");
$level3 = all_subdirectories("MainFolder/*/*");
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks @Barmar, `Parse error: syntax error, unexpected 'in'`... I've always used `as` in foreach the first time I see using `in` – Gislef Sep 24 '18 at 19:41
  • Sorry, had Python on the brain. A couple of days ago I did the opposite in a Python answer. – Barmar Sep 24 '18 at 19:43
  • :) now it's showing: `Parse error: syntax error, unexpected '{'` in line `if (!empty($next_level) {` – Gislef Sep 24 '18 at 19:48
  • Missing parenthesis, obviously. – Barmar Sep 24 '18 at 19:54
  • it was just one `)` missing... now appears to have some error in the function: `Warning: array_filter() expects parameter 1 to be array, string given in` referring to part: `return array_filter('is_dir', $files);` and other error `Fatal error: Unsupported operand types in` referring to part: `$next_level += all_subdirectories($dir);` – Gislef Sep 24 '18 at 19:55
  • Had the arguments to `array_filter` backwards. – Barmar Sep 24 '18 at 19:55
  • now it is no error, but the array is empty: `print_r($dirs);` result: `Array ( )` – Gislef Sep 24 '18 at 20:00
  • The final result is in `$all_levels` – Barmar Sep 24 '18 at 20:02
  • yeah i've already been trying `print_r($all_levels);` this too returning empty – Gislef Sep 24 '18 at 20:06
  • I just tested my code and it works. Are you sure you put the correct folder name in the initial `$dirs`? – Barmar Sep 24 '18 at 20:07
  • this worked perfectly, thank you very much, had a `/` more to put the path of the directory – Gislef Sep 24 '18 at 20:12