$request->getUploadedFiles() returns empty array on PUT

Greetings,

It seems that when using a POST request to upload a file, getUploadedFiles() behaves as expected, but when identical code is used but for using PUT requests instead of POST, getUploadedFiles() returns an empty array.

Is this for a particular reason? Is there a way to trivially access the uploaded files from a PUT request?

Thanks!

This behavior depends on the PSR-7 implementation.
Most PSR-7 implementations use the $_FILES variable to retrieve the uploaded files.
As far as I know, the $_FILES variable is only populated for POST requests.
To retrieve the file from a PUT upload, you need to manually read and parse the request stream:

$stream = fopen('php://input', 'r');

$rawPut = stream_get_contents($stream);
fclose($stream);

// parse mime content
// https://github.com/Riverline/multipart-parser#usage

You see this is quite complex. Using PUT for file uploads should be avoided because of the missing native PHP support. POST would be the better request method.

1 Like

Thank you for the detailed response. I was worried that might be the case.

I seem to regularly be running into issues with how PHP natively handles PUT requests. I understand the decision not to make opinionated decisions about architecture but I must say that from my personal perspective, it doesn’t seem very intuitive that these operations would not both have similar access to the request body— perhaps there’s something I don’t understand at the deeper level.

Anyways, thanks again.