|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49148 combination of stream_get_line and fseek does not work correctly
Submitted: 2009-08-04 07:44 UTC Modified: 2014-07-16 03:18 UTC
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: mugeso at mugeso dot com Assigned: yohgaki (profile)
Status: Closed Package: Streams related
PHP Version: 5.2.10 OS: win32 only - Windows XP SP3
Private report: No CVE-ID: None
 [2009-08-04 07:44 UTC] mugeso at mugeso dot com
When use in combination of stream_get_line and fseek,
reading a file which has only 2 lines without ending on EOF(like below) does fail.
This happens on only Windows and with only "2lines file without ending on EOF".

Reproduce code:

$file = dirname(__FILE__) . '/2lines.txt';
$fp = fopen($file, "rb");
var_dump(stream_get_line($fp, null, "\xd\xa"));
fseek($fp, ftell($fp)); // expected that this does not affect result.
var_dump(stream_get_line($fp, null, "\xd\xa"));

Expected result:
string(1) "a"
string(1) "b"

Actual result:
string(1) "a"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-31 20:04 UTC]
This is reproduceable on Linux too.

 [2010-10-15 01:22 UTC] arl at andrewrlevine dot com
I can confirm this bug (on Ubuntu using PHP 5.2.10-2ubuntu6.5 with Suhosin-Patch 
0.9.7 (cli) (built: May 21 2010 06:30:21) )

Here is a workaround function I made that seems to work:

function stream_get_line_fixed($handle, $length, $ending) {
    // Only allow 3 tries since if there's a genuine failure we don't want to 
loop forever
    $tries = 3;
    $ret = FALSE;
    while ($ret === FALSE && $tries--) {
      $ret = stream_get_line($handle, $length, $ending);
    return $ret;
 [2014-07-16 03:18 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: yohgaki
 [2014-07-16 03:18 UTC]
It seems fixed in 5.4 and later with linux. Closing.
If you have issue on Windows. Please re-open.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jul 20 20:01:28 2024 UTC