note 86633 added to function.fread

From: Date: Mon, 27 Oct 2008 12:50:28 +0000
Subject: note 86633 added to function.fread
Groups: php.notes 
Request: Send a blank email to php-notes+get-145800@lists.php.net to get a copy of this message
This code is buggy <?php $contents = ''; while (!feof($handle)) { $contents .= fread($handle, 8192); } ?> When you read a file whose size is a multiple of the readsize (8192 here), then the loop is executed when there are no more data to read. Here, the result of fread() is not checked, and so the instruction <?php $contents .= fread($handle, 8192) ?> is executed once with no data from fread(). In this very case, it is not important, but in some situation it could be harmful. The good way to read a file block by block is : <?php while ( ($buf=fread( $handle, 8192 )) != '' ) { // Here, $buf is guaranted to contain data $contents .= $buf; } if($buf===FALSE) { echo "THERE WAS AN ERROR READING\n"; } ?> ---- Server IP: 213.246.51.26 Probable Submitter: 86.67.63.208 ---- Manual Page -- http://www.php.net/manual/en/function.fread.php Edit -- https://master.php.net/note/edit/86633 Del: integrated -- https://master.php.net/note/delete/86633/integrated Del: useless -- https://master.php.net/note/delete/86633/useless Del: bad code -- https://master.php.net/note/delete/86633/bad+code Del: spam -- https://master.php.net/note/delete/86633/spam Del: non-english -- https://master.php.net/note/delete/86633/non-english Del: in docs -- https://master.php.net/note/delete/86633/in+docs Del: other reasons-- https://master.php.net/note/delete/86633 Reject -- https://master.php.net/note/reject/86633 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145800) next »