This is something silly, I'm sure, but this function is making me pull my hair out! I'm attempting to make a function to validate file extensions...if the value of variable $extension_restriction is set to 0, it should block the extensions listed in the array $extension_list. If it is set to 1, it should allow ONLY those extensions in the array.
The function:
$extension_restriction = 0; // 0 to block listed extensions, 1 to allow them
$extension_list = array("mp3","exe","vbs","php"); // Array of extensions allowed or blocked
function verify_extension($file) { // Verifies that file in question is of an allowable file type
global $extension_restriction, $extension_list;
$ext_array = explode(".",trim($file));
$ext_array_count = count($ext_array)-1;
$ext = $ext_array[$ext_array_count];
echo $ext."<br>";
if($extension_restriction){
foreach ($extension_list as $value) {
if(strtolower($value)==strtolower($ext)){
return true;
}
}
}else{
foreach ($extension_list as $value) {
if(strtolower($value)==strtolower($ext)){
return false;
}
}
}
}
When I test this function, as so:
$ex = "whatever.something.jpg";
if(verify_extension($ex)){
echo $ex.": PASS";
}else{
echo $ex.": FAIL";
}
it returns
jpg
whatever.something.jpg: FAIL
So the portion of the function where the extension is determined is functioning correctly, however the part that does the actual validating is not. Does anybody have any idea why? Thanks!