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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Mon May 27 12:01:33 2024 UTC