Forms: File Upload — HTML

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…

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:

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

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.

What you will learn