Find all .php files in folder recursively

前端 未结 4 2095
感动是毒
感动是毒 2020-12-14 13:10

Using PHP, how can I find all .php files in a folder or its subfolders (of any depth)?

相关标签:
4条回答
  • 2020-12-14 13:43

    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__));
    
    0 讨论(0)
  • 2020-12-14 13:47

    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);
    ?>
    
    0 讨论(0)
  • 2020-12-14 13:51

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 13:53

    This is similar to another answer here, but removes SKIP_DOTS as its not needed, and and works with strict_types:

    <?php
    
    $o_dir = new RecursiveDirectoryIterator('.');
    $o_iter = new RecursiveIteratorIterator($o_dir);
    
    foreach ($o_iter as $o_info) {
       if ($o_info->getExtension() == 'php') {
          echo $o_info->getPathname(), "\n";
       }
    }
    

    https://php.net/splfileinfo.getextension

    0 讨论(0)
提交回复
热议问题