15

I am trying to error handle the file_get_contents method so even if the user enters an incorrect website it will echo an error message rather then the unprofessional

Warning: file_get_contents(sidiowdiowjdiso): failed to open stream: No such file or directory in C:\xampp\htdocs\test.php on line 6

I thought if i make a try and catch it will be able to catch the error but that did not work.

try  
{  
$json = file_get_contents("sidiowdiowjdiso", true); //getting the file content
}  
catch (Exception $e)  
{  
 throw new Exception( 'Something really gone wrong', 0, $e);  
}  
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Hashey100
  • 994
  • 5
  • 22
  • 47
  • 4
    If you want to read URLs you should at very least validate that they look like URLs first, otherwise people could read files on your server. A better alternative might be using CURL – Phill Sparks Apr 21 '13 at 12:05

4 Answers4

15

Try cURL with curl_error instead of file_get_contents:

<?php
// Create a curl handle to a non-existing location
$ch = curl_init('http://404.php.net/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = '';
if( ($json = curl_exec($ch) ) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}

// Close handle
curl_close($ch);
?>
Andrey Volk
  • 3,513
  • 2
  • 17
  • 29
  • 26
    Down-voted as this is not an answer to the OP's question regarding use of file_get_contents() - offering an alternative is not a real solution. The OP asked for how to handle the errors and warnings from file_get_contents() not how to do it a completely different way. Please note that cURL is not a drop-in replacement for PHP's file_get_contents() and anyone making the move will have to severely refactor their code, hence why this is not an acceptable answer. – tpartee Dec 20 '16 at 00:48
10

file_get_contents do not throw an exception in error, instead it returns false, so you can check if the returned value is false:

$json = file_get_contents("sidiowdiowjdiso", true);
if ($json === false) {
    //There is an error opening the file
}

This way you still get the warning, if you want to remove it, you need to put an @ in front of file_get_contents. (This is considered a bad practice)

$json = @file_get_contents("sidiowdiowjdiso", true);
m4t1t0
  • 5,669
  • 3
  • 22
  • 30
  • 8
    It may be better to discuss [error_reporting()](http://uk1.php.net/manual/en/function.error-reporting.php) than promote the use of `@` – Phill Sparks Apr 21 '13 at 12:02
  • Perhaps it is worth noting that while using the `@` prefix may prevent displaying the error message to users, if you are logging errors to files using a function assigned to `set_error_handler` then you will still see the warnings logged in the files, and if you haven't then they will be included in your web server logs. – richhallstoke Nov 29 '16 at 10:54
5

You could do any of the following:

Set a global error handler (that will handle WARNINGs as well), for all of your unhandled exceptions: http://php.net/manual/en/function.set-error-handler.php

Or by checking the return value of the file_get_contents function (with the === operator, as it will return boolean false on failure), and then manage the error message accordingly, and disable the error reporting on the function by prepending a "@" like so:

$json = @file_get_contents("file", true);
if($json === false) {
// error handling
} else {
// do something with $json
}
chrisd84
  • 111
  • 3
0

As a solution to your problem please try executing following code snippet

 try  
{  
  $json = @file_get_contents("sidiowdiowjdiso", true); //getting the file content
  if($json==false)
  {
     throw new Exception( 'Something really gone wrong');  
  }
}  
catch (Exception $e)  
{  
  echo $e->getMessage();  
}  
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26