0

I'm trying to create a recursive function that returns all of the files grouped by the directory they are in. My file structure is like this

.
|-- Dir A
|   +-- file 1
|   +-- file 2
|-- Dir B
|   +-- file 11
|   +-- file 12         

and I want the resulting array to be

array[0][0] = Dir A
array[0][1] = file 1
array[0][2] = file 2

array[1][0] = Dir B
array[1][1] = file 11
array[1][2] = file 12

While the echo's I have show that it finds all of those, the final array looks like this:

array[0][0] = Dir A
array[1][0] = Dir B

So the files are not being stored in the array, or maybe overwritten, but I can't find the mistake. Would someone please point it out? My function is below.

 function GetAllFiles($dir = '.', $fileArray = '', $idx = -1){

     if ($fileArray == '') $fileArray = array();

        if(is_dir($dir)) {
         if($dh = opendir($dir)){
             while($file = readdir($dh)) {
                 if($file != '.' && $file != '..'){
                     if(is_dir($dir . $file)){
                         $idx++;
                         echo 'A '.$idx . ' - ' .$dir . $file.'<br>';
                         $fileArray[$idx][] = $dir . $file;
                         GetAllFiles($dir . $file . '/', $fileArray, $idx);
                     }else{
                         echo 'B '.$idx. ' - ' .$dir . $file.'<br>';   
                         $fileArray[$idx][] = $dir . $file;
                     }
                 }
              }
          }
          closedir($dh);         
      }

      return     $fileArray;
  }
3
  • What if in one dir is a folder and a file? Commented Oct 7, 2017 at 17:12
  • Can you post how you are calling getAllFiles() and the output/error your are getting? Commented Oct 7, 2017 at 17:14
  • The initial call is just GetAllFiles($dir); The output is shown in my question. Commented Oct 7, 2017 at 17:26

2 Answers 2

1

This works after I made a few modifications:

<?php

function GetAllFiles($dir) {
    $fileTree = [];

    if (is_dir($dir) && ($dh = opendir($dir))) {
       while ($node = readdir($dh)) {
           if (is_dir($dir . '/' . $node)) {
               if(!in_array($node, ['.', '..'])) {
                   $fileTree[] = GetAllFiles($dir . '/' . $node);
                   array_unshift($fileTree[count($fileTree)-1], $node);
               }
           } else {
               $fileTree[] = $node;
           }
        }
        closedir($dh);
    }

    return $fileTree;
}

print_r(GetAllFiles('tree'));

Given that file tree:

tree
tree/file1
tree/file2
tree/folder1
tree/folder1/file11
tree/folder1/file12
tree/folder2
tree/folder2/file21
tree/folder2/file22
tree/folder2/folder23
tree/folder2/folder23/file231
tree/folder2/folder23/file233
tree/folder2/folder23/file232

it produces that output:

Array
(
    [0] => file2
    [1] => Array
        (
            [0] => folder1
            [1] => file11
            [2] => file12
        )

    [2] => Array
        (
            [0] => folder2
            [1] => Array
                (
                    [0] => folder23
                    [1] => file231
                    [2] => file233
                    [3] => file232
                )

            [2] => file21
            [3] => file22
        )

    [3] => file1
)

which basically is:

[0] => file2
[1][0] => folder1
[1][1] => file11
[1][2] => file12
[2][0] => folder2
[2][1][0] => folder23
[2][1][1] => file231
[2][1][2] => file233
[2][1][3] => file232
[2][2] => file21
[2][3] => file22
[3] => file1
Sign up to request clarification or add additional context in comments.

Comments

0
echo 'B '.$idx. ' - ' .$dir . $file.'<br>';   
 array_push($[$idx],$dir.$file);

5 Comments

If I increment $idx on a file it will not group all of the files with the directory containing them.
?? Your code search for a directory if you try to create another dir in the main directory it will add it , but it will not add any file according to that if statement at the beginning of while loop
And the increment of idx have to be at the end of while loop
I tried moving $idx to after the while look and also at the entrance to the function - both failed. What's confusing to me is that with the code as posted, the echo's show the correct locations. It is just adding them to the array that is the problem.
I edit the code to add file in another way with array_push() try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.