note 52046 deleted from function.fread by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 10:18:19 +0000 |
| Subject: | note 52046 deleted from function.fread by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196994@lists.php.net to get a copy of this message | ||
Note Submitter: sheyh[at]nospam[]sheyh[dot]nospam[]com
----
Following code which was given as official example can lead to infinite loop.
In case of running from command line and there is no Internet connection, fopen will fail by script
timeout, however after that "!feof($handle)" will never be true.
The result will be script displaying infinite warnings and taking 10-20% cpu and it will not stop
unless killed.
<?php
$handle = fopen("http://www.example.com/",
"r");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>
I belive correct example should be:
<?php
$handle = fopen("http://www.example.com/",
"r");
$contents = '';
if($handle)
{
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
}
?>