问题描述
根据php手册或php://input,$HTTP_RAW_POST_DATA
均不适用于multipart/form-data
POST请求.
According to php manual nor php://input neither $HTTP_RAW_POST_DATA
work with multipart/form-data
POST-requests.
"php://input允许您读取原始POST数据.它是$HTTP_RAW_POST_DATA
的一种较少内存占用的替代方法,不需要任何特殊的php.ini指令.php://input不适用于."
"php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA
and does not need any special php.ini directives. php://input is not available with enctype="multipart/form-data"
."
如何获取multipart/form-data
表单的原始数据?
How can I get raw data for multipart/form-data
forms?
推荐答案
直接回答:您不能这样做.只要PHP看到multipart/form-data Content-Type,它就会坚持对其本身进行解析.原始数据将对您不可用.可悲的是.但是您可以解决它.
Direct answer: you can not do that. PHP insists on parsing it itself, whenever it sees the multipart/form-data Content-Type. The raw data will not be available to you. Sadly. But you can hack around it.
我遇到了类似的问题,一个合作伙伴正在将格式错误的数据作为multipart/form-data发送,PHP无法对其进行解析并且没有给出它,所以我可以自己对其进行解析.
I hit a similar problem, a partner was sending incorrectly formatted data as multipart/form-data, PHP could not parse it and was not giving it out so I could parse it myself.
解决方案?我将此添加到了我的apache conf中:
The solution? I added this to my apache conf:
<Location "/backend/XXX.php">
SetEnvIf Content-Type ^(multipart/form-data)(.*) NEW_CONTENT_TYPE=multipart/form-data-alternate$2 OLD_CONTENT_TYPE=$1$2
RequestHeader set Content-Type %{NEW_CONTENT_TYPE}e env=NEW_CONTENT_TYPE
</Location>
这会将传入请求的Content-Type从multipart/form-data更改为multipart/form-data-alternate,这足以阻止PHP尝试解析
This will change the Content-Type of incoming request to XXX.php from multipart/form-data to multipart/form-data-alternate, which is enough to block PHP from trying to parse it
此后,您终于可以从php://input读取全部原始数据并自己解析.
After this you can finally read the whole raw data from php://input and parse it yourself.
这很丑陋,但我没有找到更好的解决方案或实际上没有其他解决方案-只是要求对方固定立场.
It is ugly, but I have not found a better or in fact any other solution - short of asking the partner to fix their side.
NB!当您执行我在此处描述的操作时,$ _ FILES将为空.
NB! When you do what I described here, $_FILES will be empty.
这篇关于获取原始帖子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!