|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47684 file_get_contents "Content-Length" problem
Submitted: 2009-03-17 00:15 UTC Modified: 2009-04-05 14:23 UTC
From: r-ser at yandex dot ru Assigned:
Status: Closed Package: HTTP related
PHP Version: 5.2.9 OS: Linux 2.6.26
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: r-ser at yandex dot ru
New email:
PHP Version: OS:


 [2009-03-17 00:15 UTC] r-ser at yandex dot ru
When use file_get_contents + stream_context_create to post file to web server and the page in web server return redirect to another page ("location" field in answer), then file_get_contents requested redirected page with POST header without "Content-Length:" field in request and server response "HTTP/1.0 411 Length Required".
I think, it's two ways to fix problem
1) 'method' must be change to GET in requests to redirect pages.
2) Add 'Content-Length: 0' to next POST redirect requests

Reproduce code:
$argv[1] = "image.jpg";
$boundary = "AaB03x1234567890";
$type = mime_content_type($argv[1]);
$data = "--$boundary\r\nContent-Disposition: form-data; name=\"fileupload\"; filename=\"".basename($argv[1])."\"
Content-Type: ".$type." \r\nContent-Transfer-Encoding: binary\r\n\r\n".file_get_contents($argv[1])."\r\n--$boundary--";
$header = "Content-type: multipart/form-data, boundary=$boundary";
$context  = stream_context_create(array('http' => array('method'=>'POST', 'header'=> $header, 'content' => $data)));
$result = file_get_contents('', false, $context);
echo $result;

Expected result:
failed to open stream: HTTP request failed! HTTP/1.0 411 Length Required

Actual result:
Normal redirected page


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-17 11:00 UTC]
Why are you not passing the content-length in there..? I mean, isn't that the obvious error..? :)
 [2009-03-17 16:13 UTC] r-ser at yandex dot ru
I can write this code as
$header = "Content-type: multipart/form-data, boundary=$boundary\r\nContent-Length: ".strlen($data)."\r\n";
But nothing is changed.
Becouse error is in _second_ request.

cat test.php
$context  = stream_context_create(array('http' => array('method'=>'POST', 'header'=> 'Content-Length: 0', 'content' => '')));
echo file_get_contents('http://localhost/1.php', false, $context);

cat 1.php
header("Location: 2.php");

cat 2.php
echo "Method: ".$_SERVER['REQUEST_METHOD']."\n";
$headers = getallheaders();
foreach ($headers as $header => $value) echo "$header: $value\n";

Note: 1.php and 2.php are located in root of webserver

if change http://localhost/1.php to http://localhost/2.php you can see differents headers..
This works with Apache web server.
But not work with some elses, like lighttpd/1.5.0 /* like in example in first message */
 [2009-03-17 19:55 UTC] r-ser at yandex dot ru
Add string
php_stream_context_set_option(context, "http", "method", (zval *) "GET");
stream = php_stream_url_wrap_http_ex(wrapper, new_path, mode, options, opened_path, context, --redirect_max, 0 STREAMS_CC TSRMLS_CC);
In ext/standard/http_fopen_wrapper.c
Solved problem.
 [2009-03-18 10:59 UTC] r-ser at yandex dot ru
/* this message is to change status.. =) */
 [2009-04-04 16:35 UTC]
This problem had been fixed in PHP 5.3 (#45540), could you please check with PHP 5.3 ?
 [2009-04-05 14:23 UTC] r-ser at yandex dot ru
Yes,in PHP 5.3 all works fine.
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Oct 20 03:01:23 2020 UTC