Ivaalo
Ivaalo

Reputation: 82

How can file_get_contents return false without Warning error?

I'm using the PHP function file_get_contents() in order to get a string or false. However, whenever this function returns false, I get a warning message.

Warning: file_get_contents(/mypath/data/data.csv): failed to open stream: No such file or directory in mypath\inc\init.inc.php on line 10

In fact, I'm okay with this function returning false, because this file can be missing, so I don't want this specific warning message to show.

How can I hide this specific warning message? Is there a more proper way to do this?

Here is my code and how I used this function:

$csv_file = file_get_contents($csv_path); 

Upvotes: 0

Views: 650

Answers (1)

Prakash S
Prakash S

Reputation: 652

Check if the file exists before getting its contents:

if(file_exists($csv_path)){
  $csv_file = file_get_contents($csv_path);
}

Upvotes: 4

Related Questions