Sunday, April 3, 2016

PHP not process data from multipart/form-data forms

Leave a Comment

My website does no process data from forms with the enctype="multipart/form-data" set. However the 'php://input' gets the data even if the php manual says it shouldn't be available for the enctype. I think it might be some settings that is wrong but I have no idea which it might be.

Some code:

<?php     var_dump($_REQUEST);     echo file_get_contents("php://input"); ?> <form id="slideForm" action="" method="post" enctype="multipart/form-data">     <input type="text" name="test">     <input type="submit" name="submit" id="submit" value="ADD"/> </form> 

Update

I have been in contact with the support of the company that host the servers for my website and we have solved the problem. I'm not completly sure on what is the problem but it was somethin funky with their servers and php. I don't work on PHP 7.0 or PHP 5.6 but if I use their native (PHP 5.5) it works without problems.

5 Answers

Answers 1

Actually php://input allows you to read raw POST data.

It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives.

php://input is not available with enctype="multipart/form-data".

The reason is that php://input returns all the raw data after the HTTP-headers of the request, regardless of the content type.

The PHP superglobal $_POST, only is supposed to wrap data that is either

  1. application/x-www-form-urlencoded (standard content type for simple form-posts) or
  2. multipart/form-data-encoded (mostly used for file uploads)

you can use

<?php echo '<pre>'.print_r(file_get_contents($_FILES),1).'<?pre>; ?> 

Answers 2

I don't understand which type of data you want to POST. If it's just a test you should only have:

<?php     var_dump($_POST); ?> <form id="slideForm" action="" method="post">     <input type="text" name="test">     <input type="submit" name="submit" id="submit" value="ADD"/> </form> 

If you want to POST a file you must have:

<?php     var_dump($_POST); ?> <form id="slideForm" action="" method="post" enctype="multipart/form-data">     <input type="file" name="test">     <input type="submit" name="submit" id="submit" value="ADD"/> </form> 

Make sure you are able to POST data on your php server. Check these php variables:

upload_max_filesize upload_tmp_dir file_uploads 

Answers 3

In most cases you will not need to use this attribute at all. The default value (i.e. if you don't use this attribute at all) is "application/x-www-form-urlencoded", which is sufficient for almost any kind of form data. The one exception is if you want to do file uploads. In that case you should use "multipart/form-data". Try the following code for echo "test" data.

<?php     var_dump($_REQUEST);     echo $_REQUEST['test']; ?>  <form id="slideForm" action="" method="post">     <input type="text" name="test">     <input type="submit" name="submit" id="submit" value="ADD"/> </form> 

Answers 4

I tried your code, and it is working as expected: the $_REQUEST is populated properly and the php-input is empty.

Considering that your code looks fine, and it is working as expected on my server, i suggest you check your .htacces (or equivalent) for filter/rewrite modules, maybe even server config settings or even page encoding.

99% it's not the code itself.

Answers 5

<?php     // File data will display here     print_r($_FILES);     // OR you can write with condition     if($_FILES['test']){        print_r($_FILES);     } ?> <form id="slideForm" action="" method="post" enctype="multipart/form-data">enter code here     <input type="file" name="test">     <input type="submit" name="submit" id="submit" value="ADD"/> </form> 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment