BandonRandon Posted September 30, 2009 Share Posted September 30, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/176124-solved-checking-a-file-type-and-extention-from-an-array/ Share on other sites More sharing options...
RussellReal Posted October 1, 2009 Share Posted October 1, 2009 $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 } Quote Link to comment https://forums.phpfreaks.com/topic/176124-solved-checking-a-file-type-and-extention-from-an-array/#findComment-928083 Share on other sites More sharing options...
BandonRandon Posted October 1, 2009 Author Share Posted October 1, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176124-solved-checking-a-file-type-and-extention-from-an-array/#findComment-928203 Share on other sites More sharing options...
RussellReal Posted October 1, 2009 Share Posted October 1, 2009 use this instead of the foreach Quote Link to comment https://forums.phpfreaks.com/topic/176124-solved-checking-a-file-type-and-extention-from-an-array/#findComment-928225 Share on other sites More sharing options...
BandonRandon Posted October 1, 2009 Author Share Posted October 1, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/176124-solved-checking-a-file-type-and-extention-from-an-array/#findComment-928613 Share on other sites More sharing options...
RussellReal Posted October 1, 2009 Share Posted October 1, 2009 I would never wanna skin a cat lol atleast your problem is solved! Quote Link to comment https://forums.phpfreaks.com/topic/176124-solved-checking-a-file-type-and-extention-from-an-array/#findComment-928636 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.