2010-12-22 133 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。