Jump to content

[SOLVED] Checking a file type and extention from an array.


Recommended Posts

Hello,

I would like to check a file upload image type and extension to make sure it's one that is in my array of "allowed types" Here is the code that I'm currently using which halfway works. Right now the problem is it's trying to make sure the file is ALL of the image types and extensions which isn't ever gonna be the case. I guess maybe I need to find a way to say if it's one of these then stop and return true else return false. Someone suggested "!in_array" but i think this will only check either the type or the extension not both.

 

$upload_project_thum = strtolower($_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');

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';
                        $errflag = true;
        }

 

Any help would be great thanks!

$i = 0;

while ($type = each($upload_permitted_types)) {

  $e = explode(":",$type);

  if (($e[1] == $upload_project_thum_ext) && ($_FILES['upload_project_thum']['type'] == $e[0])) { $i = 1; break }

}

if (!$i) {

  // not a valid type

}

sorry for being so nieve but are you suggesting this instead of the foreach() statement or inside of it? I played with it a bit but it seams that $i is not being set to 1 if the image type and extension is inside the array.

Thanks for your help. Someone on stacked overflow suggested a solution that works which is simply to use in_array then make sure it matches both parameters.

 

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

 

As always in programing there is more than one way to skin a cat. Thanks for your help!

 

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.