|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-03-17 00:15 UTC] r-ser at yandex dot ru
Description:
------------
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('http://load.imageshack.us', false, $context);
echo $result;
Expected result:
----------------
failed to open stream: HTTP request failed! HTTP/1.0 411 Length Required
Actual result:
--------------
Normal redirected page
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 01:00:01 2025 UTC |
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 ------------ <?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 ------------ <?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 */