为什么file_get_contents()会返回错误“文件名不能为空”?

时间:2009-12-10 16:09:01

标签: php

我几乎是PHP的新手。我的背景是C / C ++和C#。我试图反对orient-ify一些简单的PHP代码,但我做错了。

班级代码:

class ConnectionString
{
  public $String = "";
  public $HostName = "";
  public $UserName = "";
  public $Password = "";
  public $Database = "";

  function LoadFromFile($FileName)
  {
    $this->String = file_get_contents($Filename);
    $Values = explode("|", $this->String);
    $this->HostName = $Values[0];
    $this->UserName = $Values[1];
    $this->Password = $Values[2];
    $this->Database = $Values[3];
  }
}

致电代码:

$ConnectionString = new ConnectionString();
$FileName = "db.conf";
$ConnectionString->LoadFromFile($FileName);
print('<p>Connection Info: ' . $Connection->String . '</p>');

我在file_get_contents($Filename)行声明错误:文件名不能为空。如果我硬编码文件名代替$ Filename,那么我只需获取字段的所有空字符串。

我错过了什么简单的概念?

5 个答案:

答案 0 :(得分:12)

你的情况有误:

file_get_contents($Filename);

应该是

file_get_contents($FileName);

您应该在php.ini文件中或使用error_reporting()

打开声明

答案 1 :(得分:3)

PHP中的变量区分大小写。您已将$FileName定义为LoadFromFile()方法的参数,但您在该方法的第一行使用了$Filename。有关PHP变量的更多信息:

http://www.php.net/manual/en/language.variables.basics.php

您可以采取一些措施来避免将来出现此问题:

答案 2 :(得分:1)

可变区分大小写:

function LoadFromFile($FileName)
{
   $this->String = file_get_contents($Filename); // This should be $FileName!

答案 3 :(得分:1)

$this->String = file_get_contents($FileName);

你有$Filename

答案 4 :(得分:1)

 $this->String = file_get_contents($Filename);

在这一行,当你应该是$ File N ame时,你写$ File n ame

相关问题