Friday 27 May 2011

Limit file upload according to file type extension with Regular Expressions

Share |
Yes its been a while since my last post. A day job keeps people busy. But nothing keeps me far from the web. The last time I saw some gibberish: /.+$/i 
What the... and its hard to search for too if you don't know that its called Regular Expressions (Doesn't seem all that very regular to me).


So, I was trying to allow users to do a file upload, but yet only restrict them to certain files types. Searching along the correct keywords and doing some quick learning did reveal some tricks. I do not seek to repeat the meaning of every expression here, this can be found easily. Rather, I will highlight a few pointers for those unacquainted.

Basically its a syntax, and the purpose is to do string matching. The combination of characters that you see in the gibberish above are actually rules to when tested upon your string of concern, results in a successful match, or a mismatch.

In the end, you realize that certain keyboard characters like full-stop (.) and the dollar sign ($) each represent a special meaning.

The full-stop: (Matches any single character except line break characters \r and \n. )
The dollar-sign after the full-stop: (.$ will return you a match for the last character in the string, and  ..$ will return you the last 2 characters in the string.)
The backslash: (If you want to actually test for a match of those special characters in your expression itself, and not use them programmatically. Used to escape those characters.)

I know it can seem confusing, but you can easily try it out here http://www.regular-expressions.info/javascriptexample.html for practice.

Finally for myself, I ended up with /\.wav\b|\.aif\b/


The \ escapes the full-stop that I want to match in my expression, and the \b means that I want a match of .wav file extension type at the word boundary of my file name (Assuming file name cannot be .wavRestOfFilename or .aifRestOfFilename). The | means a logical OR, either the left or right expression is accepted.

Therefore, I end up only matching files that end with file extension type of only  .wav or .aif.

Tada!


Quick links:
http://www.regular-expressions.info/reference.html
http://www.roblocher.com/technotes/regexp.aspx

No comments:

Post a Comment

Comments will be moderated.
You can always click 'Contact me' link to get in touch directly.