Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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: 

PHP




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


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: 

PHP




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


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:  

PHP




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


Output: 

It will redirect to GeeksforGeeks Home Page

Updated Program 2: 

PHP




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


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
 



Last Updated : 07 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads