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; } 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, 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. 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. 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 } 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. 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). Link to comment https://forums.phpfreaks.com/topic/205904-help-with-an-array/#findComment-1077649 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.