Oliver Bayes-Shelton
Oliver Bayes-Shelton

Reputation: 6292

PHP preg_match help please. Pretty simple

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)/";

Upvotes: 2

Views: 1001

Answers (3)

Mark Byers
Mark Byers

Reputation: 838106

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.

Upvotes: 4

knittl
knittl

Reputation: 265171

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))

Upvotes: 1

Nalum
Nalum

Reputation: 4213

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

Upvotes: 1

Related Questions