note 30555 added to function.fread

From: Date: Fri, 21 Mar 2003 22:26:42 +0000
Subject: note 30555 added to function.fread
Groups: php.notes 
Request: Send a blank email to php-notes+get-45621@lists.php.net to get a copy of this message
In a previous post there was an example how to read an URL file: $fp = fopen("http://www.site.com/img.jpg", "rb"); while(!feof($fp)) { $img = $img . fread($fp, 1024); } Because you can't use filesize() function here, it is used 'while(!feof($fp))'. But in some cases fopen might fail and $fp will return false. So if there is no $fp, there will be no end of the file and 'while(!feof($fp))' will cause a loop. Solving the problem is very simple - just check $fp first: if ($fp) { while (!feof($fp)) { $img = $img . fread($fp, 1024); } } -- http://www.php.net/manual/en/function.fread.php http://master.php.net/manage/user-notes.php?action=edit+30555 http://master.php.net/manage/user-notes.php?action=delete+30555 http://master.php.net/manage/user-notes.php?action=reject+30555

« previous php.notes (#45621) next »