PHP fread catch stream timeout happened

1.5k Views Asked by At

I use the PHP function fread to read the data from a stream opened with

$fh=fopen('http://.....');

i set the timeout for the stream with

socket_set_timeout($fh,10);

if a timeout happened during the fread execution then can i somehow to know about this?

$contents = fread($fh, 1024);

if timeout happens then will $contents be equal empty string or FALSE ? how to know that timeout happened? is there a way?

2

There are 2 best solutions below

0
On BEST ANSWER

According to the doc page for that function:

When the stream times out, the 'timed_out' key of the array returned by stream_get_meta_data() is set to TRUE, although no error/warning is generated.

So there are no errors/warnings generated, but inspecting the output from stream_get_meta_data will give you a clue.

0
On

fread() will return '' (empty string) when a timeout occurs unlike socket_read() which returns false.