By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,066 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,066 IT Pros & Developers. It's quick & easy.

Problem getting POST body in PHP

P: 3
So here's the situation: I need to write a PHP script which accepts form submissions using all methods (GET and POST) and all content types (application/x-www-form-url-encoded and multipart/form-data). And for POST method, it has to be able to get the exact bit-for-bit accurate contents of the POST body.

Now here's the problem: I'm stuck with POST multipart/form-data. The $GLOBALS['HTTP_RAW_POST_DATA'] only works with application/x-www-form-url-encoded. It does not work with multipart/form-data. Neither does $HTTP_RAW_POST_DATA. Same goes for using fopen('php://input', 'rb'). Attempting to read from fopen('php://stdin', 'rb') doesn't return any data at all, regardless of the request method or content-type. The PHP docs have a few clues suggesting it is possible to read from stdin without using the stream fuctions (php://), but I have not found any information on how to do this.

And here's another related dilemma: How does one create a form with a server-side image map and a file upload, and submit it to a PHP script where the script successfully accepts the file and knows the exact names and values of all the form fields? Think about it... you need to use multipart/form-data to submit forms with file uploads. And form-based server-side image maps append a ".x" and ".y" suffix to the form field name to create two more fields containing the X and Y pixel coordinates respectively of where the user clicked on the image. But PHP converts all spaces and periods/decimals/full-stops in form field names to underscores/underlines for the $_GET[], $_POST[], $_COOKIE[], and $_REQUEST[] arrays. So those ".x" and ".y" suffixes become "_x" and "_y". Further, if any form field names contain opening and closing square brackets, PHP turns them into a subarray data type! And if a lone opening square bracket is found, it too becomes an underline/underscore. Without any alternative ways of obtaining the POST data for multipart/form-data submissions, it's impossible for a PHP script to get the true names of the form fields!

There's got to be a solution to this. Any ideas?
Apr 5 '07 #1
Share this Question
Share on Google+
2 Replies

P: 3
Uh, quick update.
So using fopen('php://stdin',' rb') does actually work for getting data from stdin on GET requests... only problem is, it's not what I'm looking for. Turns out it's the source code, ie. the code fed to the PHP interpreter! (FYI I'm running PHP as a CGI, not a module.)

It doesn't return any data for POST requests of any sort, but I have a feeling from my GET experience that stdin is not something of interest to me for getting the exact bit-accurate POST body.
Apr 5 '07 #2

P: 3
And a correction:
I should have typed application/x-www-form-urlencoded in place of application/x-www-form-url-encoded. (Note the last dash/hyphen. blah.) I did have it right in my source code though.
Apr 5 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.