Accessing Raw Multipart/Form-Data POST Data
In PHP, the default behavior for multipart/form-data POST requests is for the input data to be parsed automatically by PHP. This poses a problem if you need access to the raw, unparsed data.
While both php://input and $HTTP_RAW_POST_DATA can be used to access raw POST data, neither work for multipart/form-data requests.
Workaround
Unfortunately, obtaining the raw data for multipart/form-data forms is not possible through conventional PHP methods. PHP insists on parsing the data itself, making it unavailable for manual parsing.
However, there is a workaround that involves modifying the Apache configuration. By adding the following snippet to your apache conf file, you can change the Content-Type of incoming requests:
<Location "/backend/XXX.php"> SetEnvIf Content-Type ^(multipart/form-data)(.*) NEW_CONTENT_TYPE=multipart/form-data-alternate OLD_CONTENT_TYPE= RequestHeader set Content-Type %{NEW_CONTENT_TYPE}e env=NEW_CONTENT_TYPE </Location>
This will force PHP to treat the multipart/form-data request as multipart/form-data-alternate, preventing automatic parsing. With this workaround, you can access the raw data by reading from php://input and parsing it manually.
Limitations
While this workaround allows you to access the raw data, it also has limitations. Specifically, the $_FILES superglobal will be empty, since PHP will not be parsing the form data.
The above is the detailed content of How Can I Access Raw Multipart/Form-Data POST Data in PHP?. For more information, please follow other related articles on the PHP Chinese website!