Error Handling With File_get_contents
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more file_get_contents handle 404 about Stack Overflow the company Business Learn more about hiring developers or posting ads
Php File_put_contents Catch Error
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow file_get_contents 400 bad request is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How can I handle the warning of file_get_contents() function in PHP? up
Warning File_get_contents Failed To Open Stream
vote 176 down vote favorite 55 I wrote a PHP code like this $site="http://www.google.com"; $content = file_get_content($site); echo $content; But when I remove "http://" from $site I get the following warning: Warning: file_get_contents(www.google.com) [function.file-get-contents]: failed to open stream: I tried try and catch but it didn't work. php function exception-handling warnings share|improve this question edited Aug 29 '13 at 13:05 lorem monkey 2,06622137 asked Nov 7 '08 at 15:10 file_get_contents warning Waseem 3,784113442 1 Also an interesting approach: stackoverflow.com/questions/6718598/… –Hugo Stieglitz Jun 19 '12 at 13:15 Related: stackoverflow.com/q/2002610 –Fr0zenFyr Jun 30 '15 at 19:35 add a comment| 16 Answers 16 active oldest votes up vote 286 down vote accepted Step 1: check the return code: if($content === FALSE) { // handle error here... } Step 2: suppress the warning by putting an @ in front of the file_get_contents: $content = @file_get_contents($site); share|improve this answer edited Jan 2 '14 at 16:29 answered Nov 7 '08 at 15:14 Roel 12.7k54066 45 Remember to use strict comparison: if ($content === FALSE) .If the file contains "0", then it will trigger a false negative. –Aram Kocharyan Jun 24 '11 at 3:48 3 Hi, this didn't work for me, adding @ still causes E_WARNING to be caught by some global (not mine) error handler, and my script dies before I have a chance to handle the return value. Any ideas? tnx. –Sagi Mann Nov 22 '12 at 6:51 1 There is a typo in your answer file_get_content should be file_get_contents –Khawer Zeshan Jan 2 '14 at 15:28 2 Why not use error_reporting(~E_WARNING); –irfandar May 10 '14 at 7:23 1 Though the answer is v
($latestVideos === false) { echo 'ERROR!'; } else { echo $latestVideos; } ?> Now, when this doesn't work, I get the "ERROR!" printed, but I also get a "failed to open stream: HTTP request failed!" php-error thats rather ugly... ....how can I go around this and only get the message
Php File Get Contents Failed To Open Stream
that it didnt work I want to display? Hope you guys get what I mean! Thanks
Php Warning: File_get_contents
in advance! jgetner 2011-04-07 16:06:22 UTC #2 if(!$fgc = file_get_contents(http://theurl)) { trigger_error('It\\'s all messed up'); throw new Exception('This is nice error handling'); exit('Could not file_get_contents fails silently continue); die('it is over'); } aamonkey 2011-04-07 17:11:31 UTC #3 jgetner said: if(!$fgc = file_get_contents(http://theurl)) { trigger_error('It\\'s all messed up'); throw new Exception('This is nice error handling'); exit('Could not continue); die('it is over'); } that will throw the same error. http://stackoverflow.com/questions/272361/how-can-i-handle-the-warning-of-file-get-contents-function-in-php You need to wrap that code in a conditional to check that the file exists first, e.g. $url = 'http://www.example.com'; if (file_exists($url)) { // rest of code here } jgetner 2011-04-07 17:29:44 UTC #4 i rather think your wrong my friend, See as your making a level 2 or 3 system call with file_exists and just checking the results of file_get_contents is easy and quicker and according to the results you can ether throw an error or attempt to fix the https://www.sitepoint.com/community/t/file-get-contents-error-handeling/7344 error as what ever is needed to be done. take this as some programming advice don't try to guess what your errors or bugs could be rather catch the bug/errors and attempt to fix them. aamonkey 2011-04-07 18:04:32 UTC #5 jgetner said: i rather think your wrong my friend, See as your making a level 2 or 3 system call with file_exists and just checking the results of file_get_contents is easy and quicker and according to the results you can ether throw an error or attempt to fix the error as what ever is needed to be done. take this as some programming advice don't try to guess what your errors or bugs could be rather catch the bug/errors and attempt to fix them. Sorry, I didn't understand a word of that. The point is that if you only use file_get_contents() on a file/url that doesn't exist, php throws a E_WARNING - to avoid that (and as good programming practice) test that the file exists first as I explained above. G_Schuster 2011-04-07 18:15:03 UTC #6 Suppress the warning (the @ in the code) and check as done in your current implementation - it's as simple as that. if(FALSE !== ($content = @file_get_contents('http://...'))) { //... go on } else { die('Failed'); } aamonkey said: to avoid that (and as good programming practice) test that the file exists first as I explained above. Good programming practice would be to use methods
this is your first visit, be sure to check out the FAQ by clicking the http://board.phpbuilder.com/showthread.php?10344146-RESOLVED-Try-Catch-with-File_get_contents link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum http://pc-technic.blogspot.com/2010/03/php-filegetcontents-catch-exception.html that you want to visit from the selection below. Results 1 to 7 of 7 Thread: [RESOLVED] Try/Catch with File_get_contents Tweet Thread Tools Show failed to Printable Version Email this Page… Subscribe to this Thread… Search Thread Advanced Search Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 08-16-2007,02:47 PM #1 dougal85 View Profile View Forum Posts Senior Member Join Date Apr 2006 Posts 1,349 [RESOLVED] Try/Catch with File_get_contents I'm not sure failed to open if I am using try/catch incorrectly but here is my example, PHP Code: try{
The way I see it, try catch is good for this since there is a chance the resource is external and you cant be sure that its accessible. What I don't understand is, why do you still get the "failed to open stream: HTTP request failed!" warning when the code is within the try/catch block Isn't the try/catch supposed to stop the errors being displayed then go to the catch block. Then you can choose to do what you want with the e
$xml=file_get_contents("http://www.someotherdomain.com/file.txt");
}catch(Exception$e){
echo"LoadFailed\n";
}
for a way to catch the exception from file_get_contents() function. I've had my share of trying to catch one too but the amount of code required to do so is above my level. After countless hours of wasted time I decided to simply ignore the error, like this: $a = @file_get_contents($url); @ - simply ignores the error if there is one. Nothing will happen, no output will come up. Recently, I had to use file_get_contents() function once again while appying object-oriented programming and a thought came to my head. I went to php.net website and checked how file_get_contents() works, again. This is what PHP manual says: "file_get_contents() - returns a file in a string." (We all know that) "On failure, file_get_contents() will return FALSE. " - there is the answer. If the function returns false, that means something went wrong and the result is not what you expected, so the simplest way to check if the function returned anything or not without handling/catching the exception would be to test the function in the IF statement, like this: if ( file_get_contents( $url ) != false ) { $a = file_get_contents($url); .... whatever it is that you wanted to do with the $a string } Technically you are running the function twice, but this will ensure it works or not without having to go about catching exceptions. Posted by ultimatum at 11:44 AM Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest Labels: catch exception, file get contents, file_get_contents, php, php file get contents catch exception 3 comments: ultimatumMay 13, 2010 at 9:03 AMThis comment has been removed by the author.ReplyDeleteultimatumMay 13, 2010 at 9:05 AMFor those of you that want to use exception handling then you can take this approach which will increase performance by not having to call the file_get_contents() function