Why does file upload not work without the enctype property?
The "multipart/form-data" enctype is specified by RFC 1867 which you can review here for more of a technical overview.
In HTML forms, data is represented as several fields. When using multipart/form-data as the enc type, the browser sends the form fields as a series of "parts" which each have a content-type header to describe the type of data stored in the part. This content-type is usually set to "text/plain" for normal form fields. This content-type is only sent by the browser when the multipart/form-data enctype is used.
For input elements of type "file", the content type is "application/octet-stream" or something similar which indicates to the server side software that the contents of the field are not typical plaintext but are instead the contents of a file and should be handled differently.
The reason input elements of type "file" do not work whenever "multipart/form-data" is not used is due to the fact that the server has no way of identifying that the contents of the field are any different from a normal text field (since the browser does not send the content-type unless multipart/form-data is used) so it handles the contents of the field as normal text. When the proper enctype is used and the server can properly identify what type of data the field contains, the server knows to handle the contents of the field as file data instead of text and can process it properly.
What does enctype='multipart/form-data' mean?
When you make a POST request, you have to encode the data that forms the body of the request in some way.
HTML forms provide three methods of encoding.
application/x-www-form-urlencoded
(the default)multipart/form-data
text/plain
Work was being done on adding application/json
, but that has been abandoned.
(Other encodings are possible with HTTP requests generated using other means than an HTML form submission. JSON is a common format for use with web services and some still use SOAP.)
The specifics of the formats don't matter to most developers. The important points are:
- Never use
text/plain
.
When you are writing client-side code:
- use
multipart/form-data
when your form includes any<input type="file">
elements - otherwise you can use
multipart/form-data
orapplication/x-www-form-urlencoded
butapplication/x-www-form-urlencoded
will be more efficient
When you are writing server-side code:
- Use a prewritten form handling library
Most (such as Perl's CGI->param
or the one exposed by PHP's $_POST
superglobal) will take care of the differences for you. Don't bother trying to parse the raw input received by the server.
Sometimes you will find a library that can't handle both formats. Node.js's most popular library for handling form data is body-parser which cannot handle multipart requests (but has documentation that recommends some alternatives which can).
If you are writing (or debugging) a library for parsing or generating the raw data, then you need to start worrying about the format. You might also want to know about it for interest's sake.
application/x-www-form-urlencoded
is more or less the same as a query string on the end of the URL.
multipart/form-data
is significantly more complicated but it allows entire files to be included in the data. An example of the result can be found in the HTML 4 specification.
text/plain
is introduced by HTML 5 and is useful only for debugging — from the spec: They are not reliably interpretable by computer — and I'd argue that the others combined with tools (like the Network Panel in the developer tools of most browsers) are better for that).
HTML Form With Enctype Breaks $_POST Data
To anyone who suffers the same problem, check out this page:
WAMP Uploading large File
The question gives the values you need to change in your php.ini file to allow larger file uploads (that's what was breaking the form).
HTML Form File Uploads doesn't upload file
you forgot to mention the enctype="multipart/form-data"
<form action="upload_handler.php" enctype="multipart/form-data" method="post">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>
Related Topics
CSS Property as SASS Mixin Value
Is Cross-Origin Postmessage Broken in Ie10
Html5 - Mp4 Video Does Not Play in IE9
How to Make a Chevron Arrow Using CSS
Do I Need a "/" at the End of an <Img> or <Br> Tag, etc.
Low Latency (< 2S) Live Video Streaming HTML5 Solutions
Issue with CSS Media Queries(Scrollbar)
Using HTML in Jquery UI Autocomplete
How to Pass a Parameter to HTML
Is It Considered Bad Practice to Use Absolute Positioning
How to Bevel the Corner of a Block Div
Conditional HTML Attributes Using Razor MVC3
Place Boxes Inside Ul in Center, But Align Them Left
Get Rid of Space Underneath Inline-Block Image
Setting a Width and Height on an a Tag