PHP Multiple Content-Disposition Same Name

34 views Asked by At

I am writing a client to an existing server. So I can't change the server.

For testing, I am mimicking the customer server using PHP to verify that I am posting the data correctly. Because I can get more invasive observations of the data going back and forth in my test environment.

Data is a multipart-form _POST and multiple binary image _FILES. In this case, I have to "roll my own" POST in this environment, there isn't a script or packaged HTTP client available, and it's not in a browser.

Originally in the "content-disposition" I set the 'name' and 'filename' to the same unique value. So far so good.

It was all working fine, until I was informed of a requirement that all the names of the content-disposition are "images". For example:

Content-Transfer-Encoding: binary
Content-Type: Image/Jpeg
Content-Disposition: form-data; name="images" ; filename="file1.jpg"

{yada, yada, yada}  --BOUNDARY
Content-Transfer-Encoding: binary
Content-Type: Image/Jpeg
Content-Disposition: form-data; name="images" ; filename="file2.jpg"

{yada, yada, yada}  --BOUNDARY
Content-Transfer-Encoding: binary
Content-Type: Image/Jpeg
Content-Disposition: form-data; name="images" ; filename="file3.jpg"

{yada, yada, yada}  --BOUNDARY--

At first I didn't think this was allowed. But then I found the Mozilla documentation which states

"there can be several subparts with the same name."

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

So this is "legal" but I can't get the individual files out in PHP.

I did a dump of the FILES array, shown below. When I submit a multipart-form of one _POST, and an array of multiple files, and it shows only one _FILE (specifically the last one)

[29-Jan-2024 12:20:06 America/Chicago] array(1) {   ["images"]=>  
array(5) {
    ["name"]=>
    string(46) "file2.jpg"
    ["type"]=>
    string(10) "Image/Jpeg"
    ["tmp_name"]=>
    string(31) "C:\mytemp\uploads\php9499.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(57728)   } }

I know they are all there, I even verified them by disabling the _POST and _FILES, then storing the input using:

file_get_contents('php://input');

PHP INI enable_post_data_reading

So I saved the entire content as submitted, which matches the content I sent in from the client.

How can I access the individual _FILES when they are using the same Content-Disposition name? I would prefer not to parse the raw input myself in my test PHP script.

Any advice is appreciated.

0

There are 0 answers