2011-04-28 89 views
0

getSettings()似乎只讀取並輸出目錄中的1個settings.php文件。我怎樣才能讀取並輸出所有的settings.php文件內容?PHP遞歸目錄句柄問題

<?php 
$config = array("os"=>"windows","directory"=>"../Core-Stack/Themes/","ignore"=>array('_vti_cnf','cgi-bin','.','..')); 

function getSettings($dir, $issubdir = false) { 
    global $config, $SETTINGS; 
    if ($config['os'] == "unix") 
     $delimiter = "/"; 
    else if ($config['os'] == "windows") 
     $delimiter = "\\"; 
    if (!file_exists($dir) || !is_dir($dir) || !is_readable($dir)) { 
     echo "Error: \"$dir\" is not a directory, or I cannot read it properly."; 
     return 0; 
    } else if ($od = opendir($dir)) { 
     while (($file = readdir($od)) !== false) { 
      if (!in_array($file, $config['ignore'])) { 
       $path = $dir . $delimiter . $file; 
       if (is_dir($path)) 
        getSettings($path, true); 
       elseif (is_file($path) && $file == "settings.php") 
        include ($path); 
      } 
     } 
     closedir($od); 
    } 
} 

getSettings($config['directory'], true); 
echo "Theme Name: "; 
echo $SETTINGS['theme_name']; 
echo "<br>"; 
echo "Theme Creator: "; 
echo $SETTINGS['theme_creator']; 
echo "<br>"; 
echo "Theme Version: "; 
echo $SETTINGS['theme_version']; 
echo "<br>"; 
echo "Theme Creation Date: "; 
echo $SETTINGS['theme_creation_date']; 
?> 
+3

你不需要做操作系統切你知道,你可以自己設置正確的目錄分隔符。您可以使用DIRECTORY_SEPARATOR常量,它始終包含正在運行的操作系統的正確分隔符字符序列 – GordonM 2011-04-28 13:30:16

回答

0

最有可能你的「設置」文件定義設置莫名其妙地喜歡$SETTINGS = array(...);,當然這樣你只會看到最新的包含文件內容。你可以做什麼在這裏沒有重塑整個事情是,要麼: 不改變settings.php

//... 
elseif (is_file($path) && $file == "settings.php") { 
    $OLD_SETTINGS = $SETTINGS; 
    include ($path); 
    $SETTINGS = array_merge($OLD_SETTINGS, $SETTINGS); 
} 
//... 

,或者如果你可以改變settings.php文件:

//... 
elseif (is_file($path) && $file == "settings.php") { 
    $SETTINGS = array_merge($SETTINGS, include ($path)); 
} 
//... 


//----in settings.php 
return array(
    'option' => 'foobar', 
    //... 
); 

,如果我有你的內涵是當然的對。如果沒有 - 請編輯您的問題並添加更多詳細信息。

UPDATE

也可以使用scandir以適應更小線功能,防止堆潛在的問題,如果樹是非常深的,像這樣:

function getSettings($dir, $issubdir = false) { 
    global $config, $SETTINGS; 
    if (!file_exists($dir) || !is_dir($dir) || !is_readable($dir)) { 
     echo "Error: \"$dir\" is not a directory, or I cannot read it properly."; 
     return 0; 
    } else if ($files = scandir($dir)) { 
     foreach ($files as $file) { 
      if (in_array($file, $config['ignore'])) continue; 

      $path = $dir . DIRECTORY_SEPARATOR . $file; 
      if (is_dir($path)) 
       getSettings($path, true); 
      elseif (is_file($path) && $file == "settings.php") 
       $SETTINGS = array_merge($SETTINGS, include ($path)); 
     } 
    } 
} 
0

您應該存儲目錄遞歸之前的內容,你的else-if塊應該是這樣的:

else if ($od = opendir($dir)) { 
     $subdirs = array(); 
     while (($file = readdir($od)) !== false) { 
      if (!in_array($file, $config['ignore'])) { 
       $path = $dir . $delimiter . $file; 
       if (is_dir($path)) $subdirs[] = $path; 
       elseif (is_file($path) && $file == "settings.php") include ($path); 
      }   
     } 
     closedir($od); 
     foreach($subdirs as $subdir) 
      getSettings($subdir, true); 
    } 
+0

這並不重要。這樣你只需要存儲一些內存來存儲子目錄並引入另一個循環。但通常行爲不會改變。問題一定在其他地方。 – Slava 2011-04-28 14:01:39