Find all .php files in folder recursively

You can use RecursiveDirectoryIterator and RecursiveIteratorIterator:

$di = new RecursiveDirectoryIterator(__DIR__,RecursiveDirectoryIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($di);

foreach($it as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) == "php") {
        echo $file, PHP_EOL;
    }
}

just add something like:

function listFolderFiles($dir){
    $ffs = scandir($dir);
    $i = 0;
    $list = array();
    foreach ( $ffs as $ff ){
        if ( $ff != '.' && $ff != '..' ){
            if ( strlen($ff)>=5 ) {
                if ( substr($ff, -4) == '.php' ) {
                    $list[] = $ff;
                    //echo dirname($ff) . $ff . "<br/>";
                    echo $dir.'/'.$ff.'<br/>';
                }    
            }       
            if( is_dir($dir.'/'.$ff) ) 
                    listFolderFiles($dir.'/'.$ff);
        }
    }
    return $list;
}

$files = array();
$files = listFolderFiles(dirname(__FILE__));

Very late. But I think this will be useful for those that landed here after a search.

I modified the code a bit created by supajason Because the code provided did not return a consistent result: Mainly due to the nomenclature used.

I also added some functionality.

Hope it is useful!

<?php
define('ROOT', str_replace('\\', '/', getcwd()).'/');


///########-------------------------------------------------------------
///########-------------------------------------------------------------
///######## FUNCTION TO LIST ALL FILES AND FOLDERS WITHIN A CERTAIN PATH
///########-------------------------------------------------------------
///########-------------------------------------------------------------
function listFolderFiles(
                            $dir,                   /// **** TARGET DIRECTORY TO SCAN
                            $ReturnFlat = true,     /// **** DEFAULT FLAT ARRAY TO BE RETURNED
                            $iteration  = 0         /// **** INTERNAL PARAM TO COUNT THE FUNCTIONS OWN ITERATIONS
                        ){
    ///########==================================================
    ///######## PREPARE ALL VARIABLES
    ///########==================================================
    $dir                = rtrim($dir, '/');     /// **** REMOVE TRAILING SLASH (* just for being pretty *)
    $FilesFolders       = scandir($dir);        /// **** SCAN FOR ALL FILES AND FOLDERS
    $NestedFolders      = array();              /// **** THE NESTED FOLDERS BUILD ARRAY
    static $TotalFiles  = array();              /// **** THE TOTAL FILES ARRAY

    ///######## MAKE SURE THAT THE STATIC FILES ARE WILL BE CLEARED AFTER THE FIRST ITERATION
    if($iteration === 0){
        ///######### RESET AS EMPTY ARRAY
        $TotalFiles = array();
    }
    ///########==================================================



    ///########==================================================
    ///######## RUN THROUGH ALL FILES AND FOLDERS
    ///########==================================================
    foreach($FilesFolders as $File){
        if(
            /// **** IF NOT ONE DIR UP
                ($File != '.')
            &&
            /// **** IF NOT TWO DIRS UP
                ($File != '..')
            ){
            ///######### IF IT CONCERNS A FILE
            if(is_dir($dir.'/'.$File) === true){
                $iteration++;                                                                               /// **** RAISE THE ITERATION
                $NestedFolders[]        = listFolderFiles($dir.'/'.$File, false, $iteration);    /// **** EXECUTE THE FUNCTION ITSELF
            }
            ///######### IF IT CONCERNS A FILE
            else{
                $TotalFiles[]       = $dir.'/'.$File;   /// **** ADD THE FILE TO THE TOTAL FILES ARRAY
                $NestedFolders[]    = $File;            /// **** ADD THE FILE TO THE NESTED FOLDERS ARRAY
            }
        }
    }
    ///########==================================================



    ///####### IF A FLAT LIST SHOULD BE RETURNED
    if($ReturnFlat === true){
        ///########==================================================
        ///######## RETURN ALL FILES AND FOLDERS IN A FLAT ARRAY
        ///########==================================================
        return($TotalFiles);
        ///########==================================================
    }
    ///######## IF A NESTED LIST SHOULD BE RETURNED
    else{
        ///########==================================================
        ///######## RETURN THE FILES AND FOLDERS IN A NESTED ARRAY
        ///########==================================================
        return($NestedFolders);
        ///########==================================================
    }
}

$files = listFolderFiles(ROOT, true);  /// **** FLAT ARRAY
///$files = listFolderFiles(ROOT, false);  /// **** NESTED ARRAY

echo print_r($files, true);
?>

Tags:

Php