How to handle the warning of file_get_contents() function in PHP ?

The file_get_contents() function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are supported by the server and thus enhances the performances making it a preferred way of reading contents of a file.
The path of the file to be read is sent as a parameter to the function and it returns the data read on success and FALSE on failure.

Return Value: It returns the read data on success and FALSE on failure.

Errors And Exception:

  • If you want to open a file with special characters, such as spaces, it needs to be encoded first using urlencode().
  • The file_get_contents() function returns Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.
  • An E_WARNING level error is generated if filename cannot be found, max length is less than zero, or if seeking to the specified offset in the stream fails.

Examples:

Input:  file_get_contents('https://www.geeksforgeeks.org/');
Output: A computer science portal for geeks

Input:  file_get_contents('gfg.txt', FALSE, NULL, 0, 14);
Output: A computer science portal for geeks

Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Reading contents from the
// GeeksforGeeks homepage
$homepage = file_get_contents(
echo $homepage;
  
?>

chevron_right


Runtime Errors:

PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: System error in /home/3d11f9784b99e2c83058d5842d5533ce.php on line 5
PHP Warning: file_get_contents(https://www.geeksforgeeks.org/): failed to open stream: php_network_getaddresses: getaddrinfo failed: System error in /home/3d11f9784b99e2c83058d5842d5533ce.php on line 5

Output:

It will redirect to GeeksforGeeks Home Page

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Reading 36 bytes starting from
// the 0th character of gfg.txt
$text = file_get_contents('gfg.txt',
                  FALSE, NULL, 0, 36);
echo $text;
  
?>

chevron_right


Runtime Errors:

PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: System error in /home/4659aeca06fdba457da0c5d78befb39a.php on line 6
PHP Warning: file_get_contents(gfg.txt): failed to open stream: No such file or directory in /home/4659aeca06fdba457da0c5d78befb39a.php on line 6

Output:

It will display the content of gfg.txt file.
For Example: A computer science portal for geeks

As clearly we can see above runtime errors in the form PHP Warning have occurred which were really unexpected. Here the questions arise of removing these errors, is there a way to handle these errors?
Yes, PHP provides us with a simple solution.



PHP supports one error control operator: the sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. So, above PHP Warning can be suppressed by simply inserting error control operator(@) before the function call file_get_contents() in the following way:

Updated Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Reading contents from the
// GeeksforGeeks homepage
$homepage = @file_get_contents(
echo $homepage;
  
?>

chevron_right


Output:

It will redirect to GeeksforGeeks Home Page

Updated Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Reading 36 bytes startig from
// the 0th character from gfg.txt
$text = @file_get_contents('gfg.txt',
                  FALSE, NULL, 0, 36);
echo $text;
  
?>

chevron_right


Output:

It will display the content of gfg.txt file.
For Example: A computer science portal for geeks

So, after adding the ‘@’ symbol we can see that all those PHP warnings are suppressed and only output is displayed as above.

Reference: https://www.php.net/manual/en/language.operators.errorcontrol.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.