0

I am trying to make recursive PHP function which will loop through tree of directories and subdirectories and put all directories and files in one array.

My code looks logical to me, but it is not working.

Tree of directories and files:

enter image description here

PHP code

    <?php
        function printFiles($directory) {
            $files = array();

            foreach (scandir($directory) as $file) {
                if ($file === '.' || $file === '..') 
                    continue;

                // checking is it file or directory
                if (is_dir($directory . '\\'. $file)) {
                    return printFiles($directory . '\\'.  $file);
                }

                array_push($files, $file);
            }

            return($files);
        }

        $directory = 'C:\Users\Jakov\Desktop\DIRECTORIES';
        print_r(printFiles($directory));

    ?>

I am getting this result:

Array ( )
2
  • Try echoing/logging in your function to find out what it's checking and what's being inserted. Commented Jan 12, 2018 at 15:43
  • I have also tried that. Because of some reason the program is not passing the following IF statement condition: if (is_dir($directory . '\\'. $file));. Commented Jan 12, 2018 at 15:45

1 Answer 1

2

You are only getting the files contained in the first deepest folder your function encounters, A_1_1, which has no files inside.

Don't return when you find a directory. Add to current list instead:

// checking is it file or directory
if (is_dir($directory . '/'. $file)) {
    $files = array_merge($files, printFiles($directory . '/'.  $file));
} else {
    array_push($files, $file);
}

Also, use / for paths. It works in Windows and you will avoid escaping errors:

$directory = 'C:/Users/Jakov/Desktop/DIRECTORIES';

(In your code, you should be using \\ instead of \ as separator.)

You should also check for links -and ignore them-, or you could get trapped in an infinite loop.

Sign up to request clarification or add additional context in comments.

6 Comments

I did the same thing as you told me, but I am not getting all files in array, I am getting the following result: Array ( [0] => textA_1.txt [1] => textA_2.txt [2] => text1.txt [3] => text2.txt )
Consider if you really need to store the full list of files in memory before doing something with each file. It may take a considerable amount of memory. Try using generators instead.
To me, it looks like the list of files. If you want the full path, use: array_push($files, realpath(($directory . '/'. $file));. Put the directory parsing after the files if you want "deeper" files at the end of the list.
Generators are good idea, but I need to use recursion and do it on this way and store full list of files beacause I am writing an exam and it is written in instructions.
I don't need ful path, I just need to write all files and directories in array, that is 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.