php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60465 file_get_contents doesn't stop retrieving data
Submitted: 2011-12-08 00:22 UTC Modified: 2021-09-29 10:26 UTC
Votes:6
Avg. Score:3.7 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:2 (66.7%)
From: dbc334 at gmail dot com Assigned: cmb (profile)
Status: Duplicate Package: HTTP related
PHP Version: 5.3.8 OS: Windows
Private report: No CVE-ID: None
 [2011-12-08 00:22 UTC] dbc334 at gmail dot com
Description:
------------
When I send request to script connection_close.php from web browser (Internet Explorer, Firefox or Opera), it returns only "1". connection_close.php is run on Apache 2.2 web server.

But when I execute this script from other php file via file_get_contents (eg. do_request.php), it returns "12".

Test script:
---------------
file connection_close.php:
<?php
ob_start();
print '1';
header("Connection: close");
header("Content-Length: 1");
ob_end_flush();
flush();

sleep(2);
print '2';
?>

file do_request.php:
<?php
print file_get_contents("http://web_server_path/connection_close.php");
?>

Expected result:
----------------
Executing do_request.php should return "1".

Actual result:
--------------
do_request.php returns "12".

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-13 04:14 UTC] laruence@php.net
it seems improper to use php_steam_eof while parsing header in 
php_stream_url_wrap_http_ex.
 [2011-12-13 04:14 UTC] laruence@php.net
-Status: Open +Status: Analyzed
 [2011-12-20 02:26 UTC] laruence@php.net
-Assigned To: +Assigned To: rasmus
 [2011-12-20 02:26 UTC] laruence@php.net
Rasmus, plz look at this one, thanks
 [2014-07-13 01:46 UTC] yohgaki@php.net
file_get_contents() has maxlen parameter now.
http://php.net/manual/en/function.file-get-contents.php

I guess this could be closed.
 [2014-07-13 01:52 UTC] yohgaki@php.net
-Status: Analyzed +Status: Closed
 [2014-07-13 01:52 UTC] yohgaki@php.net
Oops. This bug report is for infinite read.
Anyway, 5.4 and 5.5 finish reading it.
Closing bug.
 [2014-07-13 10:40 UTC] dbc334 at gmail dot com
This still doesn't work as it should on 5.5.14 (Windows).
 [2014-07-15 10:30 UTC] yohgaki@php.net
-Status: Closed +Status: Re-Opened
 [2014-07-26 05:17 UTC] cfowler at microsoft dot com
It seems that if you make a call to ob_status() before ob_end_flush() the expected result occurs (the remainder of the script goes unexecuted).
 [2014-07-26 12:36 UTC] dbc334 at gmail dot com
If you move ob_status() three lines downwards (so it's before ob_end_flush()), you will get warning because headers were sent after some content was flushed (this happens when output_buffering is off, which is PHP's default setting).

If you change the value of Content-Length from 1 to 2, browsers will display "12" (which is expected).

Even simpler test script:
<?php
header("Content-Length: 1");
print '12';
flush();
?>

I have run this and original test script on several servers from the browser. They gave the same results as follows:

Displayed "1" on the following configurations:
PHP 5.3.3 with Apache 2.2.15 (CentOS)
PHP 5.3.23 with Apache (unknown version, unknown linux)
PHP 5.3.28 with Apache (unknown version, unknown linux)
PHP 5.4.17 with Apache 2.2.3 (CentOS)
PHP 5.5.11 with Apache 2.4.9 (Win32)
PHP 5.5.15 with Apache 2.4.10 (Win32)

But it displayed "12" on the following configuration:
PHP 5.2.17 with Apache (unknown version, unknown linux)
(request returned header Content-Length with the value 2 instead of 1)

(I monitored all requests with Fiddler and in all requests browsers got "12" in the body of the request.)

Also: if you remove flush(), the value of Content-Length header changes from 1 to 2 and browsers display "12".
 [2015-02-01 04:26 UTC] rasmus@php.net
-Assigned To: rasmus +Assigned To:
 [2021-07-20 12:56 UTC] cmb@php.net
The problem is that the HTTP stream wrappers ignore the
Content-Length and just read until the stream is EOL.  As such, it
is the same root cause as bug #80931.
 [2021-09-29 10:26 UTC] cmb@php.net
-Status: Re-Opened +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2021-09-29 10:26 UTC] cmb@php.net
I'm closing this as duplicate of bug #80931.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:30 2024 UTC