1

Hello I am trying to make the following function iterative. It browses threw all directories and gives me all files in there.

function getFilesFromDirectory($directory, &$results = array()){
    $files = scandir($directory);

    foreach($files as $key => $value){
        $path = realpath($directory.DIRECTORY_SEPARATOR.$value);
        if(!is_dir($path)) {
            $results[] = $path;
        } else if($value != "." && $value != "..") {
            getFilesFromDirectory($path, $results);
            $results[] = $path;
        }
    }
    return $results;
}

I am sure that it is possible to make this function iterative but I really have no approach how I can do this.

8
  • 1
    I doubt so. Directory tree is a tree. You don't know how deep it is beforehand. Commented Sep 29, 2017 at 10:51
  • Definitely possible, see my answer. Commented Sep 29, 2017 at 11:30
  • It is impossible without moving the push/pop operation from a recursive function call to a data structure - i.e. you can make the code iterative (at some cost) but not the operation. What are you trying to achieve by doing this? Commented Sep 29, 2017 at 11:35
  • @Jparkinson: no - this just hides the recursion behind an object - that's why you can't call the class statically. Commented Sep 29, 2017 at 11:39
  • But does it not achieve what the question author wants? Recursion through a directory returning all paths of files within it? Commented Sep 29, 2017 at 11:41

1 Answer 1

3

Your going to want to use a few PHP base classes to implement this.

Using a RecursiveDirectoryIterator inside of a RecursiveIteratorIterator will allow you to iterate over everything within a directory regardless of how nested.

Its worth noting when looping over the $iterator below each $item is an object of type SplFileinfo. Information on this class can be found here: http://php.net/manual/en/class.splfileinfo.php

<?php 

//Iterate over a directory and add the filenames of all found children
function getFilesFromDirectory($directory){
    //Return an empty array if the directory could not be found
    if(!is_dir($directory)){
        return array();
    }

    $iterator = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($directory)
    );

    $found = array();
    foreach($iterator as $item){
        if(method_exists($item, 'isFile') && $item->isFile()){
            //Uncomment the below to exclude dot files
            //if(method_exists($item, 'isDot') && $item->isDot()){
            //  continue;
            //}
            
            //Pathname == full file path
            $found[] = $item->getPathname();
        }
    }

    return $found;
}

An var_dump of some found files i did using this function as a test:

enter image description here

Hope this helps!

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

1 Comment

Approved! Thanks for your help @JParkinson1991.

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.