How to pick up directories recursively?

Asked

Viewed 336 times

5

How do I recursively grab all files *.php? With the code below caught all that are root, but wanted to get from all directories.

I tried to use RecursiveDirectoryIterator, or use some functions I found on Soen, kind of, but nothing...

function getPageFiles()
{
    $directory = '';
    $pages = glob($directory . "*.php");
    //print each file name
    foreach ($pages as $page){
        $row[$page] = $page;
    }
    return $row;
}
  • All directories as well ? Including the base ?

  • @Edilson The files .php who are in the / (out of directories, only in /) enter the list, but those inside a directory do not (for example, files in/view/, or /model/ do not enter the list, and I need them to enter...

  • - All right, see if this helps.

  • 1

    I made a class for this, it is of specific use, but check it out - https://gist.github.com/jonataswalker/3c0c6b26eabb2e36bc90

  • @Jonataswalker really seemed nice your class, congratulations... in my specific case it will not be possible to use neither yours nor the solution that @Edilson posted, because the mini framework that I’m using for authentication is limited, and only protects the pages that are on /, so even if you can pull all the files to the list (where you set the permissions), the protection doesn’t work... For those who are interested, see the last answer of this topic. The system is super simple and easy to use, but it has this annoying limitation :/...

1 answer

6


Look, this example shows the files in layered subdirectories within the specified 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 files .php who are in the pastas/subpastas directory pointed, will be displayed.

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

  • Cool, it worked. Adapted in my code was like this: foreach ($Regex as $file){&#xA; foreach ($file as $final) {&#xA; $row[$final] = $final;&#xA; }&#xA; }&#xA; return $row;. Thanks so much! Hugs.

  • 1

    Good to know you found what you wanted, good luck buddy.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.