Jump to content

[SOLVED] regex to match image extentions suddenly not working....


dadamssg

Recommended Posts

I wrote an upload script and now my regex to check the extension(.jpg .gif .png) isn't working. This is the part thats throwing my error and telling me i don't have a valid file

if(!preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name']))  {
$_SESSION['mess'] = "Images must be JPG, GIF, or PNG.";
  extract($_POST);
   header("location: picupload.php?id={$_GET['id']}");
   exit();
}

 

help?

just tried that, not workin...this seems to work though

 

if(!preg_match('/[.](jpg)|(gif)|(png)|(jpeg)|(GIF)|(PNG)|(JPG)|(JPEG)$/', $_FILES['fupload']['name']))  {
$_SESSION['mess'] = "Images must be JPG, GIF, or PNG.";
  extract($_POST);
   header("location: picupload.php?id={$_GET['id']}");
   exit();
}

just tried that, not workin...this seems to work though

 

if(!preg_match('/[.](jpg)|(gif)|(png)|(jpeg)|(GIF)|(PNG)|(JPG)|(JPEG)$/', $_FILES['fupload']['name']))  {
$_SESSION['mess'] = "Images must be JPG, GIF, or PNG.";
  extract($_POST);
   header("location: picupload.php?id={$_GET['id']}");
   exit();
}

 

dadamssg, you are creating tons of alternation options / captures for nothing.

You can achieve the same pattern effect with this given example:

 

if(!preg_match('/\.(?:jpe?g|gif|png)$/i', $_FILES['fupload']['name']))  {

 

In essence, the entire alternation is a non capturing group due to the (?: .... ) syntax. In the jpe?g part, the e is optional due to the optional quantifier... Note (as MrAdam has done) that instead of putting the dot in a character class by itself, simply escape it instead. Also note the 'i' modifier I put after the closing delimiter.. this makes the pattern case insensitive..

So all in all, this pattern should match .jpg, .jpeg, .JPG, .JPEG, .gif, .GIF, .png and .PNG.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.