PHP error file_get_contents

I am wondering if it is possible to have an if statement something like this:

if file_get_contents() returns error code xxx OR xxx
    die();

I'm not sure how to check if an file_get_contents()error returns .

+3
source share
4 answers

It seems you want to get an HTTP status code. In this case, you need to read it here:

$http_response_header

http://php.net/manual/en/reserved.variables.httpresponseheader.php

Otherwise, if you want to get a general error, follow the deceze instructions.

+8
source

http://php.net/file_get_contents

The function returns read data or FALSE on failure.

This either works or not, as such:

$file = file_get_contents(...);
if ($file === false) {
    die();
}

PS: , , file_get_contents . HTTP URL-, cURL HTTP .

+5

How ? Php the docs , file_get_contents()will return falsefor any errors, so all you can do is

if ($content=file_get_contents($filename)===false) die("Error reading file $filename");
+1
source

you can call it that:

$data = file_get_contents("http://site.com/that.php");
if ($data !== FALSE) {
   // analyze data
}
0
source

All Articles