Skip to content

How to make sure your input field can only upload images

I had the need to have a file upload for images, so I added my little input type="file" field:

<input type="file">

I only wanted images to be allowed to be uploaded by the browser.

It’s a common thing, but I always forget how to do it.

Use the accept attribute and pass image/* to allow all images:

<input type="file" accept="image/*">

Or image/png to only accept PNG images:

<input type="file" accept="image/png">

The same syntax can be done to only accept videos:

<input type="file" accept="video/*">

or audio:

<input type="file" accept="audio/*">

Or a combination of them:

<input type="file" accept="image/*,audio/*,video/*">

Oh common thing - add multiple to allow uploading more than one:

<input type="file" multiple accept="image/*">

Of course this is only client-side validation, and you should also validate the mime type on the server when you receive the files.


→ Here's my latest YouTube video

→ Get my HTML Handbook

→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter

JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list