Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing multipart form data

Tags:

I'm trying to put together a HTML POST-ed form that has two fields--a file upload, and a text field. Since the form has a type multipart/form-data for the file upload, I can't get at the text field through the normal PHP $_POST variable. So how can I get at the text field in the form with PHP?

As per requested, here's some code, basically taken directly from Andrew:

<html>     <body>         <form action="test2.php" method="post" enctype="multipart/form-data">             Name: <input type="text" name="imageName" />             Image: <input type="file" name="image" />             <input type="submit" value="submit" />         </form>     </body> </html>  <?php   echo $_POST['imageName'];   echo "<pre>";   echo var_dump($_FILES['image']);   echo "</pre>"; ?> 

That's the entire test file. If I remove the enctype, I can get the POST-ed data, but not the file of course. With the enctype as multipart/form-data, I can get the file, but nothing from the POST-ed data.

Here's the output with the enctype:

array(5) {   ["name"]=>   string(34) "testing.png"   ["type"]=>   string(0) ""   ["tmp_name"]=>   string(0) ""   ["error"]=>   int(1)   ["size"]=>   int(0) } 

Without:

testing  NULL 

Same exact input both times.

like image 882
DashRantic Avatar asked Jul 02 '09 16:07

DashRantic


People also ask

What is multipart parser?

The Multipart parser is used for working with the request body in the multipart format. This parser creates a hash table where the names of the request body parameters are the keys and the values of the corresponding parameters are the hash table values.

What encoding is multipart form data?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.


2 Answers

File uploads come through $_FILES. Everything else comes through $_POST (assuming of course your HTML form had its method attribute set to "post").

like image 200
Sampson Avatar answered Sep 19 '22 07:09

Sampson


Check your post limit. I was going crazy trying to figure out what was causing this. A quick check to Apache error log showed that the post content length exceeded the limit. After raising the limit the post data is available.

like image 45
shane Avatar answered Sep 18 '22 07:09

shane