Say there is a form:
<form enctype="multipart/form-data" method="post">
<input name='foo'>
<input name='foo'>
<input type=submit>
</form>
posting to my php script. How (if at all) can I find out the two values submitted in these boxes. $_POST['foo'] gives the second value, but I want both.
I realize this can be done by using <input name='foo[]'> (which would make $_POST['foo'] an array). Due to certain coding decisions adding the square brackets is undesirable (read: would require some restructuring), and it seems that there should be an easy way to do this.
If the enctype was ‘application/x-www-form-urlencoded’ then parsing it would be easy, but “multipart/form-data” is more complex and could have a 30MB file in it!
Not to mention PHP has already parsed the post data.
Please can someone tell me that PHP kept that information that it parsed from $_POST and that I can access it without re-parsing.
Thanks!
You can’t access it, and
$_POSTwon’t keep it. If the browser even sent it (which it should have), the array key would be overwritten when it is parsed into the$_POSTsuperglobal from the HTTP request.It’s sort of equivalent to defining an array like:
The array key
oneis set initially, but when another value is encountered during the initialization, it is immediately overwritten.Not great for your situation (multipart form) but applicable to future readers:
If you read in the entire raw HTTP POST, you may be able to parse out the value you need.
This would give you a string like
You can’t pass it to
parse_str()though, as you’ll run into the same array key overwrite problem. You would have to do string parsing to pull out the other value.It really is easier to change them to an array with
[]if possible.