|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62773 fgets() is not binary safe
Submitted: 2012-08-07 23:23 UTC Modified: 2012-08-08 02:47 UTC
From: scott at smashcat dot org Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant 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.
Block user comment
Status: Assign to:
Bug Type:
From: scott at smashcat dot org
New email:
PHP Version: OS:


 [2012-08-07 23:23 UTC] scott at smashcat dot org
fgets() does not load the binary data for unpack to process. If the fgets() function is replaced with fread() then the example below works as expected. The current documentation claims that fgets() is binary safe, which appears to be incorrect.

Test script:

    print "Saving...\n";
    print "Loading...\n";
    print "Result: ".print_r($d,1)."\n";
    print count($d)."\n";

Expected result:
It should display an array with 6 values, as written to the file.

Actual result:
No data is read, the array is empty.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-08 02:47 UTC]
-Status: Open +Status: Not a bug
 [2012-08-08 02:47 UTC]
fgets() is binary safe, as the documentation says. However, you've given an explicit length, and to quote the manual:

"Returns a string of up to length - 1 bytes read from the file pointed to by handle. If there is no more data to read in the file pointer, then FALSE is returned."

fgets($F, 6) returns a 5 byte string, which can't be unpacked because unpack() is expecting 6 bytes per the format.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Jul 25 13:01:23 2021 UTC