2010-12-22 136 views
0

我正在嘗試讀取文件名current.conf,然後使用保存在其中的文件夾的名稱來運行opendir();當我打開:用PHP讀取.CONF文件

$file = fopen("current.conf","r"); 
$lines = fread($file,"10"); 
fclose($file); 

$lines = "/".$lines."/"; 

echo $lines; 

$dir=opendir($lines); 

$files=array(); 
while (($file=readdir($dir)) !== false) 
{ 
if ($file != "." and $file != ".." and $file != "index.php") 
{ 
array_push($files, $file); 
} 
} 
closedir($dir); 

的current.conf只有一個在這行:

2.1-2328 

我不能來打開在conf文件命名的文件夾。我有一種感覺,它與conf文件的格式有關,但不確定。

+0

opendir呼叫產生了什麼警告? – 2010-12-22 22:20:26

+0

從config中調用的目錄是否存在正確? – RageD 2010-12-22 22:26:08

回答

3

我懷疑目錄不存在(或者你沒有讀它的權利),但沒有具體的錯誤(執行opendir是最有可能拋出E_WARNING - 檢查你的日誌等)

順便說一句,可以按如下方式重新編寫代碼,以降低其複雜性:

<?php 
    // Grab the contents of the "current.conf" file, removing any linebreaks. 
    $dirPath = '/'.trim(file_get_contents('current.conf')).'/'; 

    $fileList = scandir($dirPath); 

    if(is_array($fileList)) { 
     foreach($fileList as $file) { 
      // Skip the '.' and '..' in here as required. 
      echo $file."\n"; 
     } 
    } 
    else echo $dirPath.' cound not be scanned.'; 
?> 

在這種情況下調用scandir將拋出一個E_WARNING。