Reading raw POST data in PHP

A quick tip for reading raw http POST data in PHP. For example if we have a xml posted to a page, we can read the raw data with the following code.

$xml = file_get_contents('php://input');

We could use $HTTP_RAW_POST_DATA instead, but many times it does not work due to some php.ini settings. Note that ‘php://input’ does not work with enctype=”multipart/form-data”.

9 thoughts on “Reading raw POST data in PHP

  1. In addition to you post for the people who don’t know or ask why:

    php://input does not work with multipart/form-data because of the way file uploads are streamed directly to disk

  2. “php://input does not work with multipart/form-data because of the way file uploads are streamed directly to disk”

    Which seems like a strange decision to make on behalf of PHP developers. What if I want to pump file upload data straight across from the webserver to a database or to s3 or something? Why force it to hit the local disk?
    It seems hard to find information about how to get direct access to the raw data when enctype is multipart/form-data.

  3. I’m currently going through that learning curve. I could do this easily with perl, except that I was hoping not to. I’m writing a very fancy file uploader, and I haven’t figured out how to grab the raw uploaded file size before the file is actually uploaded. Any suggestions?

Comments are closed.