2

I would like to limit the user to upload only specific files.I know it can be done through php script which the form action value is leading to but that's not what I'm looking for.

When the user is choosing a file after using that line:

<input type="file" ... />

He got an option to select all files

enter image description here

I want to change it only to a specific extensions.

Imri Persiado
  • 47
  • 2
  • 7
  • possible duplicate of [restrict file upload selection to specific types](http://stackoverflow.com/questions/7575482/restrict-file-upload-selection-to-specific-types) – JJJ Jun 12 '13 at 11:45
  • check this topics : http://stackoverflow.com/questions/8938124/how-to-filter-input-type-file-dialog-by-specific-file-type or http://stackoverflow.com/questions/3521122/html-input-type-file-apply-a-filter – scraaappy Jun 12 '13 at 11:45

1 Answers1

3

You could use the accept attribute. For example if you wanted to limit to all image files:

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

You could also specify a list of possible types:

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

Needless to say that legacy browsers that do not support this new attribute will behave exactly as shown in your screenshot and completely ignore this attribute and there's nothing you could do about it.

In all cases you should provide a server side validation of the actual file type being uploaded and never rely on the client to ensure this.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The `accept` attribute has been around since at least [HTML 3.2](http://www.w3.org/TR/REC-html32#input). – Quentin Jun 12 '13 at 11:46
  • @Quentin, good point, I didn't know that. And how do older user agents behave when they encounter it? Is this supported in older browsers? Sorry for asking but I no longer have such browsers under my hands to test. – Darin Dimitrov Jun 12 '13 at 11:49
  • It does change it to "Images only" but I have an option to change it to "All files" again and choose what ever I want. – Imri Persiado Jun 12 '13 at 11:55
  • 1
    @ImriPersiado, that's the best you can get on the client. Sorry you will have to live with that, or write your own web browser which will behave differently. Alternatively you could use some Flash plugin where this can be restricted. – Darin Dimitrov Jun 12 '13 at 11:59