Search code examples
phphttpmimemultipartform-data

Receiving multipart POST data requests in PHP


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


Solution

  • $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.