views:

423

answers:

3

Hi,

I know this has been asked a few times before. However, it's something I've had a problem with for a long time. How can I check if a file extension and mime type are in an array this is the code I currently have.

$upload_project_thum = $_FILES['upload_project_thum']['name'];
$upload_project_thum_ext = substr($upload_project_thum, strrpos($upload_project_thum, '.') + 1);    
$upload_permitted_types= array('image/jpeg:jpg','image/pjpeg:jpg','image/gif:gif','image/png:png');

Then down where i'm checking if the file is a valid type I have this foreach loop

foreach ($upload_permitted_types as $image_type) {
     $type = explode(":", $image_type);
      if (($type[0] != $_FILES['upload_project_thum']['type']) &&  ($upload_project_thum_ext != $type[1]) ) {
       $errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project thumbnail'. $type[1] . " Type: ". $type[0];
       $errflag = true;
        }

The problem is that if the file type isn't ALL of the types in the array (which is impossible) I get an error. It works to the point where if the upload file is in the array that error message won't trigger.

Any help would be great!

+1  A: 
if (!in_array($_FILES['upload_project_thum']['type'], $upload_permitted_types)){

   exit("Unsupported file type");
}
rubayeet
Won't this only check that the mime type is correct and thus I would need to change my array to only contain mime types. Also, wouldn't the $_FILES['upload_project_thum'] have to be $_FILES['upload_project_thum'] [type]. I'm still learning so I'm just asking. Thanks for your help!
BandonRandon
Edited the answer. If you want check on extensions, then the first parameter of in_array() would be the extension of you uploaded file, and the second would be an array of all allowable extensions.
rubayeet
so you are saying have two arrays one for extension and one for mime type? Then use two in_arrays? I can see how that could work. Thanks for your help.
BandonRandon
+1  A: 
if( !in_array( $_FILES['upload_project_thum']['type'] . ':' . $upload_project_thum_ext, $upload_permitted_types) ) {
    Trigger-error-here;
}

This should look for a proper string glued from both the type and extension.

Another way is to modify your loop like that:

$is_allowed = false;
foreach ($upload_permitted_types as $image_type) {
    $type = explode(":", $image_type);
    if (($type[0] == $_FILES['upload_project_thum']['type']) && ($type[1] == $upload_project_thum_ext ) ) {
        $is_allowed = true;
        break;
    }
}

if( !$is_allowed ) {
        $errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project thumbnail'. $type[1] . " Type: ". $type[0];
        $errflag = true;
}
macbirdie
Thanks, this seems to work. I'm using your first method. You're second method is something others have also suggested and I'm sure it would work but it's just more lines of code. Thanks for your help!
BandonRandon
+1  A: 

The way I am doing this now is:

    $upload_permitted_types['mime']= array('image/jpeg','image/gif','image/png');
$upload_permitted_types['ext']= array('jpeg','jpg','gif','png');

if(!in_array($_FILES['upload_project_thum']['type'],$upload_permitted_types['mime']) || !in_array($upload_project_thum_ext,$upload_permitted_types['ext'])
{
       $errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project thumbnail';
       $errflag = true;
}

The advantage to this is that it will allow a .gif file with a mime of jpeg. So it doesn't force the mine and the extension to match but does make sure they are both image types.

BandonRandon