[英]Unable to access HTTP PUT data in webservice code
All, 所有,
As part of an application I'm writing I need to have a HTTP PUT webservice which accepts incoming imagedata, which will by analyzed, validated, and added to a local file store. 作为我正在编写的应用程序的一部分,我需要有一个HTTP PUT Web服务,该服务接受传入的图像数据,该数据将经过分析,验证并添加到本地文件存储中。
My issue arises after the size validation as the 我的问题出现在尺寸验证后,因为
$_SERVER['CONTENT_LENGTH']
has a > 0 value, and this value is identical to the test file size, so I can assume that all is going well up to this point but when I try to read the incoming stream data using 的值大于0,并且该值与测试文件的大小相同,因此我可以假设一切顺利, 但是当我尝试使用读取流数据时
file_get_contents('php://stdin');
I get an empty string. 我得到一个空字符串。 I've also tried using
我也尝试过使用
file_get_contents('php://input');
And this give me the same result of an empty string. 这给了我一个空字符串的相同结果。
Any help, suggestions or direction will be appreciated. 任何帮助,建议或指示将不胜感激。
NB: I'm using 注意:我正在使用
Apache HTTPD denies PUT requests by default. 默认情况下,Apache HTTPD拒绝PUT请求。 You might check out mod_put:
您可以签出mod_put:
http://perso.ec-lyon.fr/lyonel.vincent/apache/mod_put.html http://perso.ec-lyon.fr/lyonel.vincent/apache/mod_put.html
and add this to httpd.conf: 并将其添加到httpd.conf中:
<Location /upload/dir> EnablePut On AuthType Basic AuthName "Web publishing" AuthUserFile /www/etc/passwd AuthGroupFile /www/etc/group <Limit PUT> require valid-user </Limit> </Location>
My best guess is that you need to alter httpd.conf to not deny PUT requests. 我最好的猜测是您需要更改httpd.conf才能不拒绝PUT请求。 Have you checked that?
你检查了吗?
The r
you've got as the second arg isn't right. 您作为第二个参数获得的
r
不正确。 file_get_contents
doesn't use the a/r/w/a+/r+/w+
arguments that fopen
uses. file_get_contents
不使用fopen
使用的a/r/w/a+/r+/w+
参数。 You probably want to remove it and just do: 您可能要删除它,然后执行以下操作:
file_get_contents('php://input');
See http://us3.php.net/file_get_contents . 参见http://us3.php.net/file_get_contents 。
file_get_contents
doesn't take the "r" parameter - see the PHP manual page : file_get_contents
不使用“ r”参数-请参见PHP手册页 :
string file_get_contents ( string $filename [, int $flags...)
Valid $flag
values are FILE_USE_INCLUDE_PATH, FILE_TEXT, FILE_BINARY
. 有效的
$flag
值为FILE_USE_INCLUDE_PATH, FILE_TEXT, FILE_BINARY
。
Try removing the "r" flag and trying again 尝试删除“ r”标志,然后重试
Edit - question updated, "r" flag was being ignored so evidently not the root of the problem. 编辑 -问题已更新,“ r”标志已被忽略,因此显然不是问题的根源。
It looks like there's a reported bug in PHP regarding file_get_contents
returning an empty string for a HTTP POST. 似乎PHP中有一个关于
file_get_contents
返回HTTP POST空字符串的错误 。 From the bug description: 从错误描述:
file_get_contents('php://input')
(and alsofile, fopen+fread
) does not return POST data, when submited form with enctype="multipart/form-data".当使用enctype =“ multipart / form-data”
file, fopen+fread
表单时file, fopen+fread
file_get_contents('php://input')
(以及file, fopen+fread
)不返回POST数据。When submited the same form without enctype specified (so default "application/x-www-form-urlencoded" is used) all works OK.
当提交未指定enctype的相同表单时(因此使用默认的“ application / x-www-form-urlencoded”),一切正常。
So it looks like a work-around is to change the specified form enctype away from multipart/form-data
, which obviously isn't ideal for an image upload - from the W3 FORM specification : 因此,似乎一种解决方法是将指定的表单enctype更改为
multipart/form-data
,这显然不适合图像上传-从W3 FORM规范开始 :
The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters.
内容类型“ application / x-www-form-urlencoded”对于发送大量二进制数据或包含非ASCII字符的文本效率不高。 The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
内容类型“ multipart / form-data”应用于提交包含文件,非ASCII数据和二进制数据的表单。
Further Edit 进一步编辑
This bug seems to have been resolved in your PHP version. 您的PHP版本似乎已解决此错误。 Have you checked to make sure that the buffer being read in doesn't start with a carriage-return / newline char?
您是否检查过以确保读取的缓冲区不是以回车/换行符开头? There's a problem somewhat similar to yours that was discussed on Sitepoint .
在Sitepoint上讨论了一个与您有些类似的问题。
Try running strlen
on the input and see what the length is. 尝试在输入上运行
strlen
,看看长度是多少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.