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

07 May, 2021
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(      "https://www.geeksforgeeks.org/");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(      "https://www.geeksforgeeks.org/");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 
arorakashish0911
PHP-Misc
Picked
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
Comparing two dates in PHP
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?