Php file_get_contents handle error

php file_get_contents handle error

As for 2), you would normally use a try / catch block. Unfortunately, file_get_contents() is one of those old PHP functions that don't throw catchable. I am trying to fetch an image using file_get_contents function but it gives an error. To handle the error I am using try catch block but it does not catch. Best Solution. Step 1: check the return code: if($content === FALSE) { // handle error here }. php file_get_contents handle error

Php file_get_contents handle error - that

MYSQLI_REPORT_STRICT);try{ $xml = file_get_contents("sprers.eu"); } catch(Exception $e){ echo "Load Failed\n"; } if(!$xml = file_get_contents('sprers.eu')) { print 'Load Failed.'; }function myFetchContents($file) { if(!$xml = file_get_contents($file)) { throw new Exception('Load Failed'); } } // to call try { myFetchContents('sprers.eu'); } catch(Exception $e) { print $e->getMessage(); }$contents = file_get_contents("sprers.eu"); if ($contents === FALSE) { // failed! do something here }

Unable to catch PHP file_get_contents error using try catch block

//set your own error handler before the call set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context) { throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line ); }, E_WARNING); try { $url = 'sprers.eu'; $file_content = file_get_contents($url); } catch (Exception $e) { echo 'Error Caught'; } //restore the previous error handler restore_error_handler();

Next Lesson PHP Tutorial

Main Categories

Popular Search