note 30555 deleted from function.fread by vincent

From: Date: Sat, 16 Aug 2003 13:52:18 +0000
Subject: note 30555 deleted from function.fread by vincent
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-54352@lists.php.net to get a copy of this message
Note Submitter: alexanderangelov@abv.bg ---- 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); } }

« previous php.notes (#54352) next »