General Question

bumpley's avatar

Html forms, how to strict the browse button to a certain file type?

Asked by bumpley (10points) October 31st, 2008

.. also limiting it to a single selection would be great also.

Observing members: 0 Composing members: 0

4 Answers

lapilofu's avatar

An HTML file submit selection is always limited to only one file, as far as I’m aware. There is no simple way of restricting the selected file to a single filetype. You’ll have to write a PHP validation script that checks the filetype before processing the form. You could probably also do it with a javascript, but that would be immensely insecure.

bumpley's avatar

thanks for the response. I do, however, have php validating all input but i was kind of hoping that there would be a way (without resorting to js) in the earliest possible stage to force the users to browse for the correct file type… in this case images.

philo23's avatar

Well firstly Javascript would be pointless to use on the file input, as its disabled for security reasons on pretty much every modern browser.

To do what your actually talking about, restricting it to certain file types on the file browsing window it self, you’ll want to look into the “accept” attribute, which you can specify a mimetype to restrict it to certain files. (you might also be able to seperate them with something, i think a semicolon to do multiple file types)

Information on the accept attribute .

EDIT: i forgot to mention, you should not use only this for checking the file type, some browsers dont support it.

wenbert's avatar

javascript cannot “touch” files in the user’s machine—that would be a security nightmare. imagine opening a web page that has malicious JavaScript in it.

i think there is now way (for now), for you to restrict the files that show up in the “Browse” button list. as what lapilofu said, you can use PHP to restrict illegal/unwanted file names during the upload.

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther