dadamssg Posted August 24, 2009 Share Posted August 24, 2009 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? Quote Link to comment Share on other sites More sharing options...
Adam Posted August 24, 2009 Share Posted August 24, 2009 Would need to be: /\.(jpg|gif|png)$/ Quote Link to comment Share on other sites More sharing options...
dadamssg Posted August 24, 2009 Author Share Posted August 24, 2009 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(); } Quote Link to comment Share on other sites More sharing options...
Adam Posted August 24, 2009 Share Posted August 24, 2009 Works here... <?php $file = 'my_image.jpg'; if (!preg_match('/\.(jpg|gif|png)$/', $file)) { echo 'Invalid!'; } else { echo 'Valid!'; } ?> Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 24, 2009 Share Posted August 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.