With HTML, how do I limit what kind of filetypes can be uploaded?
To easy the user experience, I want to limit file uploads to be only images (jpeg, gif, png).
<form method="post" action="..." enctype="multipart/form-data">
<label for="image">Photo</label>
<input name="image" type="file" />
</form>
HTML5 says
<input type="file" accept="image/*">
. Of course, never trust client-side validation: Always check again on the server-side...Checkout a project called Uploadify. http://www.uploadify.com/
It's a Flash + jQuery based file uploader. This uses Flash's file selection dialog, which gives you the ability to filter file types, select multiple files at the same time, etc.
Ultimately, the filter that is displayed in the Browse window is set by the browser. You can specify all of the filters you want in the Accept attribute, but you have no guarantee that your user's browser will adhere to it.
Your best bet is to do some kind of filtering in the back end on the server.
Edited
If things were as they SHOULD be, you could do this via the "Accept" attribute.
http://www.webmasterworld.com/forum21/6310.htm
However, browsers pretty much ignore this, so this is irrelavant. The short answer is, i don't think there is a way to do it in HTML. You'd have to check it server-side instead.
The following older post has some information that could help you with alternatives.
File input 'accept' attribute - is it useful?
HTML5 File input has accept attribute and also multiple attribute. By using multiple attribute you can upload multiple images in an instance.
You can also limit multiple mime types.
and another way of checking mime type using file object.
file object gives you name,size and type.
You can also restrict the user for some file types to upload by the above code.
You can only do this securely on the server-side. Using the "accept" attribute is good, but must also be validated on the server side lest users be able to cURL to your script without that limitation.
I suggest that you: discard any non-image file, warn the user, and redisplay the form.