views:

187

answers:

3

Hi ,

How do I add into the list of items allowed (gif$|jpg$|png$|jpeg$)

into the following code

$regexp = "/[0-9a-zA-z\.]/";
if (preg_match($regexp, $imageInput))

also how do I add in an exclude list e.g so stop people unploading .exe files etc.

Thanks

EDIT

I fixed it with the following code

any advice on how to make it better ?

$regexp = "/^[a-zA-z0-9._-]+(gif|jpg|png|jpeg)/";
+3  A: 

Use a positive list:

/^[0-9a-zA-z\.]+\.(gif|jpg|png|jpeg)$/

Or a negative lookahead:

/^[0-9a-zA-z\.]+\.(?!exe$)[a-z]+$/

A positive list is the safer, but more restrictive option.

Mark Byers
That wouldn’t allow `foo.execute` neither.
Gumbo
@Gumbo: Thanks, that can be fixed by using (?!exe$).
Mark Byers
+1  A: 

change your regex to /([0-9a-zA-z\.])(gif|jpg|png|jpeg)$/ this will only allow for the extensions you have specified.

Nalum
+1  A: 

try:

$regexp = "/^[0-9a-zA-z\.]+\.(jpg|gif|png)$/";

for a stopping-pattern use '/.(exe|cmd|bat|sh)$/' and then if(!preg_match($pattern, $text))

knittl
Thanks for the help i actually fixed it by myself after a few minutes apart from it thanks :)
Oliver Bayes-Shelton