Imagine asking a friend for some information on a webpage. The field should explain exactly what you need.
What You Need to Know
The accept hint can narrow files shown in the picker; the actual MIME type must be checked after upload.
Try the Example
Take a look at the example. We’ll explain the important lines right below it.
<form action="/upload" method="post" enctype="multipart/form-data">
<label for="attachment">Attach a PDF file</label>
<input id="attachment" name="attachment" type="file" accept="application/pdf" required>
<button type="submit">Upload file</button>
</form>
Let’s Read the Code
- accept: Accept suggests which files should be selectable; it is not file security validation.
- <form>: The form groups controls and identifies where submitted data goes.
- <label>: The label tells people what the input is for. A matching for/id pair makes the label clickable.
- for: For connects a label or output with a control by ID.
Read the named elements and attributes together. They explain how the browser connects this feature to the rest of the page.
What Happens in the Browser?
The HTML controls work in the browser, but saving an answer requires the example action path to be handled by a real server.
What happens to the file? Choosing a file does not upload it immediately. The browser sends it when the form is submitted to an endpoint that accepts file uploads. The server must check the file type, size, and permissions before storing it.
Your Turn
Change accept to image/png and observe how the file picker filters files.
Common Beginner Mistakes
- Copying the example without giving the fields a clear label and a usable name.
- Filenames and extensions can be changed, so they are not trustworthy proof of file contents.
Key Takeaways
- A control needs a clear label and, when submitted, a meaningful name and value.
- accept: Accept suggests which files should be selectable; it is not file security validation.
- Remember that HTML form rules help the visitor, but server-side code must decide whether received data is valid.