Forum Moderators: open

Message Too Old, No Replies

Javascript file extension regex

         

MrFahad

4:05 am on Feb 11, 2009 (gmt 0)

10+ Year Member



Hello, does anyone know how to make file extension regex note: my users upload photo from their harddisk so we need to validate the path

Thank you

coopster

11:55 pm on Feb 13, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What are you trying to validate, the <input type="file"> value? Have you tried it yet?
[webmasterworld.com...]

rocknbil

7:37 pm on Feb 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello, does anyone know how to make file extension regex note: my users upload photo from their harddisk so we need to validate the path

First, since this is in the Javascript forum, I'll guess that you hope to do this with Javascript.

This won't be possible, because you want to read the value of the file input box after they've browsed. This is disallowed by browsers for user security. Imagine if you could, it would lead to information about the user's computer and file structure, and that would be a security issue.

So the only real solution here is to do a regex on the server site, using your server side language. But even that is not a complete solution - what's to stop me from saving my malicious virus (.exe) as .jpg?

The best thing to do is use a graphics interface like GD or ImageMagick that actually reads the uploaded file. Get the image type from the graphic interface, if it's one of your valid types you're good to go. This also releases you from improperly named files (or, in the case of Macintosh, no extensions at all.)

Both Perl and PHP have interfaces to these programs, pretty sure a tweak can be added for Windows-based servers and programming.