php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67371 fread does not return error (false) while reading out of file
Submitted: 2014-06-02 07:47 UTC Modified: 2014-06-02 09:12 UTC
From: roman4e at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.5.13 OS: linux mint 15
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: roman4e at gmail dot com
New email:
PHP Version: OS:

 

 [2014-06-02 07:47 UTC] roman4e at gmail dot com
Description:
------------
php v.5.5.5 (cli) (built: Mar 17 2014 12:31:03) (DEBUG)

fread() does not return error (false) on read out of file, returns "" (empty string)

Test script:
---------------
$f = fopen("file","r"); // file must be several KBytes size
$page = 0;
while ( !fseek($f,$page,SEEK_SET) )
{
   $page += 1024;
   $str = fread($f,1024);
   if ( $str === false )
      die("error reading block");
}

echo "ok"

Expected result:
----------------
fread should return false

Actual result:
--------------
file size of 76800
result of reading out of file. last parametr is type of data returned by fread
Pageoff = 75776 fseek=0 ftell=75776 read=1024 string
Pageoff = 76800 fseek=0 ftell=76800 read=0 string
Pageoff = 77824 fseek=0 ftell=77824 read=0 string

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-06-02 08:31 UTC] mike@php.net
-Status: Open +Status: Not a bug
 [2014-06-02 08:31 UTC] mike@php.net
This is not a bug, reading past EOF is not an error case; read(2) states:

"If the current file offset is at or past the end of file, no bytes are read, and read() returns zero."
 [2014-06-02 09:03 UTC] roman4e at gmail dot com
There is one string in PHP manual for fread():

Reading stops as soon as one of the following conditions is met:
- EOF (end of file) is reached

So it SHOULD return false, when EOF is reached.
 [2014-06-02 09:12 UTC] mike@php.net
You clear EOF with fseek, see your other bug.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 02:01:32 2025 UTC