Why does file_get_contents () return the error "The file name cannot be empty"?

I am pretty new to PHP. My background is C / C ++ and C #. I am trying to create an object orientational PHP code, but I am doing something wrong.

Class Code:

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]; } } 

Call Code:

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

I get an ann error in the line file_get_contents($Filename) stating: the file name cannot be empty. If I hardcode the file name instead of $ Filename, then I just get all the empty lines for the fields.

What simple concept am I missing?

+6
php
source share
5 answers

You have the wrong code:

 file_get_contents($Filename); 

it should be

 file_get_contents($Filename); 

You must enable Notifications, either in the php.ini file or using error_reporting ()

+12
source share

Variables in PHP are case sensitive. You defined $FileName as a parameter to the LoadFromFile() method, but you used $FileName in the first line of this method. Additional information about PHP variables:

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

There are several things you can do to avoid this problem in the future:

  • Use IDEs such as Eclipse PDTs that support automatic completion of variables.
  • Configure error_reporting to display all types of errors ( E_ALL ).
+3
source share

Variable case sensitivity:

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

you have $Filename

+1
source share
  $this->String = file_get_contents($Filename); 

In this line you write $ File n when it should be $ File N ame

+1
source share

All Articles