PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Caching » Cache_Lite » Bug #4236

fread PHP warning on zero length read

Details

Submitted2005-04-27 17:59 UTC
Fromrick dot overman at gmail dot com
Assignedfab
StatusClosed
PackageCache_Lite
PHP Version4.3.10
OSLinux
Roadmaps(Not assigned)

Comments

[2005-04-27 17:59 UTC] rick dot overman at gmail dot com

Description:
------------
In Lite.php line 638
$data = @fread($fp, $length);

Comment from http://www.php.net/fread
-------------------------------------
Somewhere between 4.2.3 and 4.3.9, the behaviour of fread() was changed slightly.

When you ask to fread() 0 bytes of data, it spits out a warning (IMO for no good reason. I think reading 0 bytes is perfectly valid, if not very useful.)

Consequently, to write warning-free code using fread, you'd have to take code like this:

$content=fread($fd,filesize($myfile));

and replace it with this:

$content='';
$length=filesize($myfile);
if($length) {
$content = fread($fd, $length);
}

...or this, depending on your taste:

$length=filesize($myfile);
if($length) {
$content = fread($fd, $length);
} else {
$content='';
}

Reproduce code:
---------------
Read an empty cache.

Expected result:
----------------
No PHP warning if cache empty.

Actual result:
--------------
Produces a PHP Warning.