|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49838 feof() reached end while reading big HTTP response from socket using fgets.
Submitted: 2009-10-11 17:08 UTC Modified: 2009-11-02 01:00 UTC
Avg. Score:4.2 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:1 (33.3%)
From: travian dot utils at gmail dot com Assigned:
Status: No Feedback Package: Sockets related
PHP Version: 5.2.11 OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-10-11 17:08 UTC] travian dot utils at gmail dot com
feof() reached end of stream while reading big HTTP response from socket using fgets.

Reproduce code:
  $fp = @fsockopen ($sname, 80, $errno, $errstr, 18);
  if ($fp) {
    fputs ($fp, "GET /".$xxx." HTTP/1.0\r\nHost: ".$sname."\r\nUser-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv: Gecko/2008092313 Firefox/3.1.6\r\n\r\n");
    while (!feof($fp)) {
      if(strpos($line, '404 Not Found')!=false){$http_code=404; break;};
      if($substr2=='Location: '){$redirect=substr($line,10);break;};
      if($substr3=='Content-Length: ')$len=intval(substr($line,16));
      if($substr=='Last-Modified: ')$time=substr($line,15);
// This cycle reached end while reading big HTTP response 
    while (!feof($fp)) {
      return -10;
      return -2;
      return -3;
      return -4;
      return -5;
        return 0;
    return -6;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-12 11:51 UTC] gmc at sonologic dot nl
I'm experiencing the same problem on:

FreeBSD 7.1-PRERELEASE FreeBSD 7.1-PRERELEASE #0: Tue Oct 14 11:55:05 CEST 2008     root@postel:/usr/obj/usr/src/sys/GENERIC  i386

The problem is that feof($fp) returns TRUE, even though the end of the remote file has not been reached.

The below script reproduces the problem. I'm sorry it depends on an external resource, that's just the nature of the problem. AFAICS, the bug is not dependent on the particular file chosen, it occurs for any large text file.



        $file = "";

        if (!($fp = fopen($file, "r"))) {
                die("could not open XML input from $file");

        while($data=fgets($fp,10000)) {
                if($trigger_bug) {
                  print "chunk $chunkno, total ".$total." (+".strlen($data)."), eof: ".(feof($fp)?1:0)."<br/>\n";
                } else {
                  print "chunk $chunkno, total ".$total." (+".strlen($data).")<br/>\n";


With $trigger_bug set to true, this will terminate before all of the file is read. With $trigger_bug set to false it will read the entire file. Note that the only difference between the two is that the script displays the output of feof($fp).

This bug started to bite us when we upgraded from 5.2.1 to 5.2.11, it is not present in 5.2.1 / 5.1.4.
 [2009-10-12 19:17 UTC] travian dot utils at gmail dot com
Hello, Sjoerd.

Sorry for not fully clear description.

Thank you for interest to this problem.
gmc(at)sonologic(dot)nl understood my description and put code for reproducing this problem. Thanks also to him.

Problem occurred when we updated PHP from 5.2.10 to 5.2.11 on our production.
On PHP 5.2.10 problem code on the same server worked properly.
We tested problem code on the same server using PHP version 5.2.8 and also code worked properly.

Now we rewrote our code for downloading big HTTP files without using fgets(using socket_create, socket_bind, socket_connect, socket_select, socket_write, socket_read and socket_close).
It works fine on PHP 5.2.8 and 5.2.11.

And it working fine in production on PHP 5.2.11.
So this is bug, not a configuration issue.

With best regards, 
 [2009-10-12 21:42 UTC]
Verified (with gmc at sonologic dot nl's script), 5.2.11 only
 [2009-10-24 00:58 UTC]
this issue is no longer reproducible with latest svn snapshot ( pl. verify with latest snapshot and let us know. 
 [2009-10-25 20:31 UTC]
Please try using this snapshot:
For Windows:

 [2009-11-02 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 25 12:01:32 2024 UTC