用PHP读取.CONF文件

用PHP读取.CONF文件,php,Php,我正在尝试读取一个文件名current.conf,然后将其中保存的文件夹名用于opendir();当我打开时: $file = fopen("current.conf","r"); $lines = fread($file,"10"); fclose($file); $lines = "/".$lines."/"; echo $lines; $dir=opendir($lines); $files=array(); while (($file=readdir($dir)) !== fal

我正在尝试读取一个文件名current.conf,然后将其中保存的文件夹名用于opendir();当我打开时:

$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文件的格式有关,但不确定

我怀疑该目录不存在(或者您没有读取该目录的权限),但没有特定错误(opendir很可能抛出E_警告-检查您的日志等)

顺便说一句,您可以按如下方式重新编写代码以降低其复杂性:

<?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.';
?>


在本例中,对的调用将抛出E_警告

我怀疑该目录不存在(或者您没有读取该目录的权限),但没有特定错误(opendir很可能抛出E_警告-检查您的日志等)

顺便说一句,您可以按如下方式重新编写代码以降低其复杂性:

<?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.';
?>


在本例中,对的调用将抛出E_警告

opendir调用产生了什么警告?从配置中调用的目录存在正确吗?@middaparka没有错误,我想请参见下面的注释@RageD是的,它确实存在。opendir调用产生了什么警告?从配置调用的目录存在正确吗?@middaparka无错误,我想请参见下面的评论@是的,它确实存在。我将文件夹权限更改为777,所有者更改为root。但我也使用了上面的代码。我去掉了开头的“/”(那是我的错),但现在它起作用了。因为我一次做了所有这些,所以不确定实际的修复是什么。但是谢谢你提出了缩短代码的好主意。我把文件夹权限改为777,所有者改为root。但我也使用了上面的代码。我去掉了开头的“/”(那是我的错),但现在它起作用了。因为我一次做了所有这些,所以不确定实际的修复是什么。但是谢谢你提出了缩短代码的好主意。