How to get directories recursively?

5

How do I recursively get all *.php files? With the code below I get everyone who is root, but I wanted to get it from all the directories.

I tried using RecursiveDirectoryIterator , or use some of the functions I found in SOen, type this , but nothing ...

function getPageFiles()
{
    $directory = '';
    $pages = glob($directory . "*.php");
    //print each file name
    foreach ($pages as $page){
        $row[$page] = $page;
    }
    return $row;
}
    
asked by anonymous 11.09.2015 / 02:24

1 answer

6

Look, this example shows the files in subdirectories layered within the indicated scope.

<?php

$dir_iterator = new RecursiveDirectoryIterator("../");
    $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
    $Regex = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);


    foreach ($Regex as $file) {
        foreach($file as $final){
            echo $final, "<br/>";   
        }
    }

?>   

In this case, all .php files that are in pastas/subpastas of the pointed directory will be displayed.

On the PHP.net page, there are usually examples in the notes at the bottom of the page. Although this is an example of it, I've added a specific line to do what you wanted. I hope it's useful to you.

    
11.09.2015 / 03:07