PHP not process data from multipart/form-data forms - php

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.

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

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>

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.

<?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>

When you send the header Content-Type:multipart/form-data; PHP parses the data from request into $_POST and $_FILES and clears the input buffer. In order to read the input buffer you should remove that header.

Related

How to send file using http protocol without encode?

I want to send a png file to my apache server and export it using wireshark.
I tried a html form like this:
<form action="." enctype="multipart/form-data" method="post">
<p>files: <input type="file" name="datafile" size="40"></p>
<div>
<input type="submit" value="submit">
</div>
</form>
But i can't export my file as PNG. I think it happens because I use "multipart/form-data". I've heard that this encrypts the file. I'm not sure, what do you think I should try?

How to run php on a button click

So I have been browsing trough a lot of threads, and the only suitable solution for my situation I found was this:
PHP:
<?php
if(isset($_POST["submit"])) {
my php code goes here
}
?>
HTML:
<form id="usp-form-23" class="usp-form" method="post" enctype="multipart/form-data" action="" data-validate="parsley" data-persist="garlic" novalidate>
<input name="usp-title" type="text" value="" data-required="true" required="required" maxlength="99999" placeholder="Answer" class="usp-input usp-input-title" />
<input name="submit" id="submit" type="submit" class="usp-submit usp-submit-default" value="Send Message" />
</form>
But it doesn't work.
Maybe I'm missing something?
Need help.
P.S. I can't use an external php file. Only php code inside the same file, because of the variables I need to grab from the post.
I also can't use $_GET, because if a person refreshes the page, my php will be repeated, which I don't want. I only need to run my php once.

Why can't I use action="example.php" on angular to connect my form to my php file

<formn action="" method="POST">
<label>Name</label>
<input type="name">
<button name="submit">Submit</button>
</form>
I have also tried
<form ngNoForm action="table.php" method="POST">
<label>Name</label>
<input type="name">
<button name="submit">Submit</button>
</form>
but I keep on getting this error
"Cannot POST /table.php"
I have double checked and even triple checked the spelling and its right.
It's likely the path for the file is wrong. In your first example you used backend/table.php, give that a shot (example below).
<form ngNoForm action="backend/table.php" method="POST">
<label>Name</label>
<input type="text" name="name">
<input type="submit" value="Submit" />
</form>
But more importantly, if you want to submit a POST request through a form you have to set the URL you want to POST to as the form's action. Putting a link around a button will never submit the form.
<button name="submit">Submit</button>
This would just redirect you to backend/table.php, no POST request was sent so no data would be processed.

I can't get this piece of code to work -> $_SERVER['PHP_SELF'] <-

I'm trying to run a very simple script to test the "action=echo $_SERVER['PHP_SELF'];" in a HTML form (with php start and end on it but this site doesn't allow me to introduce those chars). This is the script:
<?php if(isset($_POST['submit'])) { echo "it works"; } ?>
<form name="test" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="submit" value="Submit Form">
Problem is whenever I hit the submit button I get a "Safari can't find the file" error stating that "no file exists at the address" where I run the script.
What am I doing wrong here?
Many thanks in advance for your help!
Eduardo
Try using $_SERVER['REQUEST_URI'] instead of $_SERVER['PHP_SELF']. I think you are using mod_rewrite and that's why it cannot find the file.
You don't need to set the action attribute of the form, if you want to post data to the same page.
<form name="test" method="post">
...
<input type="submit" name="submit" value="Submit Form" />
</form>
should do it.
Cheers!

Using reCAPTCHA and enctype="multipart/form-data" together

I have a file upload form on which I also want to use the Google reCAPTCHA.
If I have the following
<form method="post">
<input type="file" name="filename">
<div class="g-recaptcha" data-sitekey="***"></div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
the I can use the reCAPTCHA, but cannot upload the file.
However, if I use:
<form method="post" enctype="multipart/form-data">
<input type="file" name="filename">
<div class="g-recaptcha" data-sitekey="***"></div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
I can upload the file successfully, and use the reCAPTCHA provided that the file is a text file. I cannot get it to work if I try to upload a pdf file.
When uploading a text file, the $_POST contains the 'g-recaptcha-response', but when uploading a pdf, the $_POST does not contain the 'g-recaptcha-response'.
Can someone explain what is going wrong here?
EDIT
It seems like it's actually a filesize problem.
Files larger than ~200kb cannot be sent whatever their format.
I have upload_max_filesize = 2M in my php.ini file, so I'm not sure why 200kb is too large...
Any thoughts?
EDIT 2: More information
It looks like the $_FILE contains the error code 3: UPLOAD_ERR_PARTIAL.
I don't see why the file cannot be uploaded completely.
EDIT 3: Getting somewhere
I can now upload files. It seems like I need to put the reCAPTCHA before the file input.
<form method="post" enctype="multipart/form-data">
<div class="g-recaptcha" data-sitekey="***"></div>
<input type="file" name="filename">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
Can anyone elaborate on why this might be the case?
Edit 4: Spoke too soon
Switching the order makes the g-recaptcha-response' appear in $_POST, but I am still getting the error code 3: UPLOAD_ERR_PARTIAL.
Edit 5
It looks like the file is being uploaded correctly, since I can see it in the header parameters (firefox debugger). It seems like php is just not filling in the $_FILE array properly...
I tried the same code on a different server and it worked fine.
Looks like the problem was that the server I set up has some sort of configuration issue. It actually has nothing to do with the interaction of reCAPTCHA and the file input + enctype.
I will update if I figure out what the configuration problem is.

Categories