php.net |  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
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: michael dot virnstein at brodos dot de
New email:
PHP Version: OS:

 

 [2006-11-08 17:26 UTC] michael dot virnstein at brodos dot de
Description:
------------
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:
---------------
<?php

$fp = fopen('http://www.php.net', 'r')
  or die ('could not connect');

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

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

fclose($fp);


?>

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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-08 17:52 UTC] tony2001@php.net
http://php.net/fgets
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-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC