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?

Link to comment
Share on other sites

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();
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.