Changing your profile photo, sending a payment receipt to an online shop: both need a button for picking a file from your phone or laptop. In HTML, you make that button with <input type="file">. This tag shows a "Choose File" button.
The button alone is not enough. For the file's contents to really reach the server (the computer that receives the form's data), the form needs one extra attribute called enctype.
An upload form, part by part
<form method="post" enctype="multipart/form-data">
<label for="photo">Profile photo</label>
<input type="file" id="photo" name="photo" accept="image/*">
<button type="submit">Upload</button>
</form>
From top to bottom:
<form ...>wraps every field and the Upload button. An input outside the<form>is not sent, and a button outside it doesn't send anything.method="post"means the data is sent inside the request (the message from the browser to the server). Ifmethod="post"is missing, the form adds its data to the end of the page's address, and a file can't be sent that way. So an upload form must usepost.enctype="multipart/form-data"sets how the form packs its data. A form's default packing can only carry text. So without this attribute, only the file name arrives. Withmultipart/form-data, the file's contents are sent too. Type it exactly like that:multipart, a slash,form-data.<input type="file">is the "Choose File" button. Like other inputs, it has no closing tag.name="photo"is the name of this data when it reaches the server, the same as on other inputs. Without aname, the server can't find the file.accept="image/*"limits which kinds of files can be picked.image/means images, and*means any kind of image: JPG, PNG, WebP.<button type="submit">is the send button: a click sends the form to the server. It must be inside the same<form>as the input.
The result: click "Choose File" and the file picker window shows only images. Pick a photo, click Upload, and the photo is sent to the server.
Choose file types with accept
The value of accept can be a general kind or a file name ending:
accept="image/*": any image.accept="video/*"oraccept="audio/*": any video or any audio.accept=".pdf": only PDFs. Separate several kinds with commas, for exampleaccept=".pdf,.docx".
accept only filters what the file picker shows. In that window, visitors can still change the filter to "All files" and pick another kind of file. So the server still has to check the type and size of every file that arrives. The server also has its own size limit: PHP (a language often used on servers), for example, accepts files up to 2 MB by default, and a larger file does not arrive even when the form is correct.
The capture attribute on <input type="file"> works on phones: capture="environment" opens the back camera right away, for example to take a photo of a receipt, and capture="user" opens the front camera. On a laptop, this attribute is ignored.
Several files at once with multiple
<input type="file" name="gallery" accept="image/*" multiple>
multiple is written without a value, just its name. With this attribute, visitors can pick several files at once. Without multiple, they can pick only one file.
⚠️ Files that never arrive, and a filter mistaken for security
- A missing
enctype. The form is still sent, but only the file name arrives. The file's contents do not. - A misspelled
enctype. The browser doesn't recognisemultipart/formdataormultipart-form-data. So it uses the default packing again, which only carries text. - A missing
name. The file was picked, but the server doesn't know which name to look for. - The button outside
</form>. The button isn't connected to the form. So when someone clicks it, it sends nothing. - Treating
acceptas security.acceptis only a filter that helps visitors pick the right kind of file. The real check of the file is still the server's job.
TL;DR: Visitors pick a file with <input type="file" name="...">. The form uses method="post" and enctype="multipart/form-data". Without them, only the file name is sent, not the file's contents. accept="image/*" filters the file types, and multiple allows several files. The file input and a type="submit" button go inside the same <form>. accept is only a filter, so the server still checks every file.
⚡ A button for picking a file
Write:
<form method="post" enctype="multipart/form-data">
<label for="photo">Profile photo</label>
<input type="file" id="photo" name="photo" accept="image/*">
<button type="submit">Upload</button>
</form>
Meaning: <input type="file"> shows a "Choose File" button. name is the name the server uses to find the file. The input and the send button, <button type="submit">, must be inside the same <form>.
⚡ The file itself comes along
Guess first: without enctype, what reaches the server: the file's contents, or only its name?
Only the file name. Without enctype, the form's default packing can only carry text.
Meaning: An upload form uses method="post" and enctype="multipart/form-data", typed exactly like that. With both, the file's contents are sent too.
⚡ Filter the type, pick several
Write:
<input type="file" name="gallery" accept="image/*" multiple>
Meaning: accept="image/*" makes the file picker show only images. accept is only a filter, not security, so the server still checks every file. multiple is written without a value, and allows several files at once.