|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39433 fgets doesn't work as expected
Submitted: 2006-11-08 17:26 UTC Modified: 2006-11-09 14:22 UTC
From: michael dot virnstein at brodos dot de Assigned:
Status: Not a bug Package: Streams related
PHP Version: 4.4.4 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
49 - 46 = ?
Subscribe to this entry?

 [2006-11-08 17:26 UTC] michael dot virnstein at brodos dot de
i recognized a change to fgets function, which i think is a bug. 
When i try to fetch data from a file handle via fgets, which has been opened via fopen for a local file or url, the lenght parameter now has to be supplied as length+1 to get the desired result.
E.g. fgets($fp, 1) returns false now, fgets($fp, 2) returns the first letter, fgets($fp, 3) returns the first two letters ...
This bug is present in 4.4.4 and 5.1.x, couldn't test 5.2 yet.

Reproduce code:

$fp = fopen('', 'r')
  or die ('could not connect');

var_dump(fgets($fp, 1)); // returns false

var_dump(fgets($fp, 2)); // returns first letter



Expected result:
- first var_dump should return first letter
- second var_dump should return next two letter

Actual result:
- first var_dump returns false
- second var_dump returns first letter


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-08 17:52 UTC]
Returns a string of up to length - 1 bytes read from the file pointed to by handle. 

It works this way since ages.
 [2006-11-09 14:22 UTC] michael dot virnstein at brodos dot de
Ok, but it didn't seem to work that way in PHP 4.2.x. I moved a script from a server with 4.2.1 to one with PHP 5.1.6 and the script stopped working.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jul 12 18:01:29 2024 UTC