Reading raw POST data in PHP


Posted in: php | Save to del.icio.us | Twit This! 12 Sep 2008

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”.




Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts



4 Responses

1

Trii

November 13th, 2008 at 4:25 pm

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

julian

June 4th, 2009 at 12:41 am

“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

Dave

July 11th, 2009 at 7:59 pm

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?

4

Hardik

July 27th, 2009 at 12:18 am

Thanks its working fine now.

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]
  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]
  • Martin: How can you carry over your own variables into the URL? I am using a form to POST a couple of var [...]
  • nancy: thanks very much ! first tools [...]

  • Users Online

    • 13 Users Online
    • 11 Guests, 2 Bots