note 52046 added to function.fread

From: Date: Tue, 19 Apr 2005 23:39:53 +0000
Subject: note 52046 added to function.fread
Groups: php.notes 
Request: Send a blank email to php-notes+get-88231@lists.php.net to get a copy of this message
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); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.fread.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52046 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52046&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52046&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52046&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52046&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52046&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52046&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88231) next »