在 PHP 中接收多部分 POST 数据请求
我想在 PHP:
中接收以下 HTTP
请求
Content-type: multipart/form-data;boundary=main_boundary
--main_boundary
Content-type: text/xml
<?xml version='1.0'?>
<content>
Some content goes here
</content>
--main_boundary
Content-type: multipart/mixed;boundary=sub_boundary
--sub_boundary
Content-type: application/octet-stream
File A contents
--sub_boundary
Content-type: application/octet-stream
File B contents
--sub_boundary
--main_boundary--
(注意:我缩进子部分只是为了使其对本文更具可读性。)
我不是对 PHP 非常流利,希望获得一些帮助/指示来弄清楚如何在 PHP 代码中接收这种多部分表单请求。 我曾经编写过一些代码,其中收到了一个标准 HTML 表单,然后我可以通过使用表单元素的名称作为 $HTTP_GET_VARS
数组中的索引键来访问表单元素,但在本例中没有表单元素名称和表单数据部分不是线性的(即子部分=多级数组)。
感谢您的帮助!
/罗伯特
I want to receive the following HTTP
request in PHP:
Content-type: multipart/form-data;boundary=main_boundary
--main_boundary
Content-type: text/xml
<?xml version='1.0'?>
<content>
Some content goes here
</content>
--main_boundary
Content-type: multipart/mixed;boundary=sub_boundary
--sub_boundary
Content-type: application/octet-stream
File A contents
--sub_boundary
Content-type: application/octet-stream
File B contents
--sub_boundary
--main_boundary--
(Note: I have indented the sub-parts only to make it more readable for this post.)
I'm not very fluent in PHP and would like to get some help/pointers to figure out how to receive this kind of multipart form request in PHP code. I have once written some code where I received a standard HTML form and then I could access the form elements by using their name as index key in the $HTTP_GET_VARS
array, but in this case there are no form element names, and the form data parts are not linear (i.e. sub parts = multilevel array).
Grateful for any help!
/Robert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该注意:
“php://input 允许您读取原始 POST 数据。 它是 $HTTP_RAW_POST_DATA 的内存密集型替代方案,并且不需要任何特殊的 php.ini 指令。 php://input 不可用 enctype=”multipart/form-data”
从 php 手册...所以看起来 php://input 不可用
还不能发表评论,但这旨在补充 pilsetnieks 的答案
You should note:
“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”
From php manual... so it seems php://input is not available
Cannot comment yet but this is intened to complement pilsetnieks answer
上传的文件可以通过 $_FILE 全局变量访问,其他参数可以通过 $_GET 全局变量访问。
Uploadeds files will be accessible through the $_FILE global variable, other parameters will be accessible trough the $_GET global variable.
$HTTP_GET_VARS
、$HTTP_POST_VARS
等是过时的表示法,您应该使用$_GET
、$_POST
现在,文件内容应该位于$_FILES
全局数组中,而如果没有元素名称,我不确定其余内容是否会显示在 <代码>$_POST。 无论如何,如果 php.ini 中的
always_populate_raw_post_data
设置为 true,则数据应位于$HTTP_RAW_POST_DATA
中。 此外,在读取 php://input 时,整个请求应该显示出来。$HTTP_GET_VARS
,$HTTP_POST_VARS
, etc. is an obsolete notation, you should be using$_GET
,$_POST
, etc.Now, the file contents should be in the
$_FILES
global array, whereas, if there are no element names, I'm not sure about whether the rest of the content will show up in$_POST
. Anyway, ifalways_populate_raw_post_data
setting is true in php.ini, the data should be in$HTTP_RAW_POST_DATA
. Also, the whole request should show up when reading php://input.