How do I POST an array with multipart/form-data encoding? How do I POST an array with multipart/form-data encoding? php php

How do I POST an array with multipart/form-data encoding?


If you want an associated array you can pass index in a name of a form field:

Content-Type: multipart/form-data; boundary=--abc--abcContent-Disposition: form-data; name="name[first]"first value--abcContent-Disposition: form-data; name="name[second]"second value

Then on php level print_r($_POST) would give you

Array ( [name] => Array ( [first] => 'first value', [second] => 'second value' ) )

If you are after just a normal ordered array then same as you did:

Content-Type: multipart/form-data; boundary=--abc--abcContent-Disposition: form-data; name="name[]"first index--abcContent-Disposition: form-data; name="name[]"second index

Then on php level print_r($_POST) would give you

Array ( [name] => Array ( [0] => 'first index', [1] => 'second index' ) )

Params with [] in their names translating into arrays on a server side is a feature specific to PHP (http://www.php.net/manual/en/faq.html.php#faq.html.arrays).

As for multipart encoding you can find more in RFC: http://www.ietf.org/rfc/rfc1867.txt