refiking Posted June 26, 2010 Share Posted June 26, 2010 I want to have an array of extension types that are acceptable. Currently, the method I am using will only allow me to check 1. What should I do differently? $ext = substr($file_name, strrpos($file_name, '.') + 1); if($ext != 'jpg'){ $exterror++; } if($exterror > 0){ echo "Error:File type not supported."; return; } Quote Link to comment https://forums.phpfreaks.com/topic/205904-help-with-an-array/ Share on other sites More sharing options...
bluejay002 Posted June 26, 2010 Share Posted June 26, 2010 try this: $ext_array("jpg", "png"); // so on and so forth $ext = substr($file_name, strrpos($file_name, '.') + 1); if(!in_array($ext, $ext_array)){ $exterror++; } if($exterror > 0){ echo "Error:File type not supported."; return; } hope this helps. bluejay, Quote Link to comment https://forums.phpfreaks.com/topic/205904-help-with-an-array/#findComment-1077476 Share on other sites More sharing options...
JasonLewis Posted June 26, 2010 Share Posted June 26, 2010 I do it like this: $allowed = array("jpg","gif","bmp"); if(!in_array(array_pop(explode(".", $file)), $allowed)){ // not a valid file type }else{ // valid file type } Fairly similar to bluejay002s. Quote Link to comment https://forums.phpfreaks.com/topic/205904-help-with-an-array/#findComment-1077495 Share on other sites More sharing options...
brianlange Posted June 26, 2010 Share Posted June 26, 2010 if you have a large array make the extensions keys and use array_key_exists. Php can search keys faster than values. Quote Link to comment https://forums.phpfreaks.com/topic/205904-help-with-an-array/#findComment-1077520 Share on other sites More sharing options...
Alex Posted June 26, 2010 Share Posted June 26, 2010 You can also do it like this (using the built in function to get the extension): $allowed = array("jpg", "gif", "bmp"); if(in_array(pathinfo($path, PATHINFO_EXTENSION), $allowed)) { // valid } else { // not } Quote Link to comment https://forums.phpfreaks.com/topic/205904-help-with-an-array/#findComment-1077525 Share on other sites More sharing options...
bluejay002 Posted June 26, 2010 Share Posted June 26, 2010 @projectFear: well, its not entirely my code. I just assume his code were correct and added my code on it. I'm kinda lazy writing a new code over something that the user didn't complained about. Quote Link to comment https://forums.phpfreaks.com/topic/205904-help-with-an-array/#findComment-1077593 Share on other sites More sharing options...
JasonLewis Posted June 26, 2010 Share Posted June 26, 2010 @projectFear: well, its not entirely my code. I just assume his code were correct and added my code on it. I'm kinda lazy writing a new code over something that the user didn't complained about. True enough, but hopefully the OP can look at modified (and cleaned) code and see any improvements (or maybe it got worse). Quote Link to comment https://forums.phpfreaks.com/topic/205904-help-with-an-array/#findComment-1077649 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.