2015-10-16 45 views
1

我有以下腳本旨在採取給定類型的任何和所有文件,並返回所有值的數組。但是,當我運行腳本時,它不會添加模塊文件夾中的任何目錄中的值,除非我只是添加後續數組。模塊讀取器不加載所有模塊

<?php 

function get_modules($dir,$ftype) { 
    $file = scandir($dir); 

    $result = array(); 

    foreach($file as $key => $value) { 
     if($value == "." || $value == "..") { 
      // Do Nothing 
     } else { 
      if(is_dir($dir . "/" . $value)) { 
       array_merge($result, get_modules($dir . "/" . $value, $ftype)); 
      } else { 
       if(pathinfo($value,PATHINFO_EXTENSION) == $ftype) { 
        array_push($result, $dir . "/" . $value); 
       } else { 
        // Do Nothing 
       } 
      } 
     } 
    } 

    return $result; 
} 

$modules = get_modules("modules","txt"); 

print_r($modules); 

?> 

回答

2

爲什麼不使用glob() with some modifications?見

if (! function_exists('glob_recursive')) { 
    // Does not support flag GLOB_BRACE 
    function glob_recursive($pattern, $flags = 0) { 
     $files = glob($pattern, $flags); 
     foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) 
      $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags)); 
     return $files; 
    } 
} 

function get_modules($ftype) { 
    $result = glob_recursive($ftype); 
    return $result; 
} 

$modules = get_modules("*.txt"); 
print_r($modules); 
+0

這幾乎是完美的一切需要的是一種方法,第一個目錄限制爲單個選項謝謝你,我不知道這個功能的 – Jdoonan

+0

高興這奏效了! – Jan

+0

是的,只是一個小的修改,以刪除「./」並確認模塊目錄,它的完美無瑕滿足我的需求 – Jdoonan