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.
Here's how I did it... No need for try-catch block... The best solution is always the simplest... Enjoy!
You can also set your error handler as an anonymous function that calls an Exception and use a try / catch on that exception.
Seems like a lot of code to catch one little error, but if you're using exceptions throughout your app, you would only need to do this once, way at the top (in an included config file, for instance), and it will convert all your errors to Exceptions throughout.
You should use file_exists() function before to use file_get_contents(). With this way you'll avoid the php warning.
You should also set the
in your
php.ini
to stop receiving warnings.