php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #3515 fread() just read a few bytes
Submitted: 2000-02-17 09:15 UTC Modified: 2002-11-15 00:54 UTC
From: bjarne dot ingelsson at enator dot se Assigned: hholzgra (profile)
Status: Closed Package: Documentation problem
PHP Version: 4 OS: WinNT4 SP6
Private report: No CVE-ID: None
 [2000-02-17 09:15 UTC] bjarne dot ingelsson at enator dot se
// PHP4b3 problem! 
// This version is not available in the dropdown...


fread() doesn't return the correct number of bytes in Win32 environment.

For some reason it just reads the first 33 bytes (in my example, depends on the file to be read).


$sourcefile = "./aniclick.gif";
$fd = fopen($sourcefile, "r");
$contents = fread($fd, filesize($sourcefile));
$encoded =  chunk_split(base64_encode($contents));
fclose($fd);
	
echo "FileSize: ". filesize($sourcefile)."<BR> 
Read: ".strlen($contents). "<BR> 
Encoded: ".strlen($encoded). "<BR> 
Ratio: ". number_format((strlen($encoded) / strlen($contents)), 2). "<BR>"; 


The code above give the following result:

FileSize: 19910 
Read: 33 
Encoded: 46 
Ratio: 1.39 

/Bjarne

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-01 15:06 UTC] hholzgra@php.net
documentation should tell that fread may return early due to signal or other cause of interrupt, should give example using feof() ...
 [2002-11-01 06:09 UTC] jaenecke@php.net
As explained in the documentation the mode for fopen() should contain a 'b' 
when accessing binary files on windows systems 
 
 [2002-11-15 00:54 UTC] philip@php.net
The documentation now mentions this binary related information.  Here's the diff by sniper on Tue Mar 6 18:43:03 2001:

http://cvs.php.net/co.php/phpdoc/en/functions/filesystem.xml?r=1.53

An example also exists using feof().
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 21:01:31 2025 UTC