php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #22883 feof($fp) where $fp isn't a valid resource returns false
Submitted: 2003-03-25 17:32 UTC Modified: 2003-03-27 21:02 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: stephen at mu dot com dot au Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 4.3.0 OS: Linux
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: stephen at mu dot com dot au
New email:
PHP Version: OS:

 

 [2003-03-25 17:32 UTC] stephen at mu dot com dot au
Short code snippet -

$fp = fopen($filename);
while (!feof($fp)) fread($fp, 1024);

Summary -
fopen() requires 2 arguments, not 1 so it returns NULL (this is correct behavior).

feof($fp) when $fp = NULL returns false. So when using !feof($fp) as a loop condition, we get an infinite loop (and 2 error messages per iteration around the loop).

I suggest that the foef() function be modified to return that foef($fp) is true, not false, when reporting an error with the file handle resource. This is on the basis that foef($fp) returning false is a condition for the program to continue reading from $fp (i.e. the file pointer contains more data - has not reached the end of file).

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-27 21:02 UTC] pollita@php.net
Your summary is not 100% correct.

feof(NULL) will actually do two things:

1) It will generate an E_WARNING condition stating that the supplied argument is not a valid stream resource.

2) It will return NULL, not FALSE

This means two things:

1) replacing: (!feof($fp)) with a sctrict check of (feof($fp) === false) will be enough to 'fix' your example script.

2) Good coding practices dictate that one should examine the contents of a variable before using it.

$fp = fopen($filename);
if ($fp) {
  while (!feof($fp)) fread($fp, 1024);
} else {
  echo 'Unable to open file.';
}

Either way, there is no reason to modify this function with a piece of bloat which performs an otherwise unnecessary operation.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC