2013-02-24 170 views

回答

11

只需添加類似:

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__)); 
31

您可以使用RecursiveDirectoryIteratorRecursiveIteratorIterator

$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; 
    } 
} 
3

很晚。 但我認爲這對搜索後落在這裏的人會有用。

我修改了一下創建的代碼通過supajason 由於提供的代碼沒有返回一致的結果:主要是由於所使用的術語 。

我還添加了一些功能。

希望它是有用的!

<?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

*「提供的代碼沒有返回一致的結果」* - 這是什麼意思?你的實現行爲有什麼不同? – 2015-11-04 15:32:36

相關問題