Receiving multipart POST data requests in PHP

2019-08-02 20:59发布

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

3条回答
beautiful°
2楼-- · 2019-08-02 21:10

$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, if always_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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-08-02 21:14

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

查看更多
孤傲高冷的网名
4楼-- · 2019-08-02 21:16

Uploadeds files will be accessible through the $_FILE global variable, other parameters will be accessible trough the $_GET global variable.

查看更多
登录 后发表回答