How do you detect that a stream resource is no longer valid? I have a script that apparently kills, maims, or threatens a little fsockek_open() connection resource so that it often goes away with no warning. Below are the var_dumps from my script.
resource(6) of type (stream)
resource(6) of type (stream)
resource(6) of type (stream)
resource(6) of type (Unknown) <-- causes feof() error on next check because it's not valid
Basic checks like if($resource) return TRUE which causes problems when checking like this.
if($resource AND !feof($resource))
The first boolean check on the resource works, but the feof fails causing an error because the stream is now unknown when the second half of the conditional is tested.
PHP Warning: feof(): 6 is not a valid stream resource
Which makes examples like this not work.
while ($resource AND ! feof($resource)) <-- fails with error
{
$buffer .= fread($resource);
}
fclose($resource);
I thought I had tried this, but a simple resource check seems to catch the problem in it’s tracks.