Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Check if file type is valid



4:30 pm on Apr 20, 2007 (gmt 0)

10+ Year Member

I'm uploading files onto my site and I need to determine if the extension is one from the following array, AND determine the filetype using the same array.

$validTypes = array(
"app" => array("jad", "jar", "mpn"),
"img" => array("gif", "jpg", "jpeg", "png", "thm", "tif", "tiff"),
"snd" => array("aac", "amr", "mid", "mmf", "mp3"),
"txt" => array("ini", "nfo", "txt"),
"vid" => array("3g2", "3gp", "mp4", "mpg")

The question is, how do I traverse this array and return:
- If the extension was found on list
- What type (app, img, snd...) was the file

Thanks in advance. I totally suck with arrays on PHP!


6:09 pm on Apr 20, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

You've set up $validTypes as an array of arrays, so you need to check the extension against each of the arrays in it.
$found = false;
foreach($validTypes as $type_name => $type_array) {
$found = true;
} // EndIf found type
} // EndForEach allowed type
if($found) {
echo 'Type is' . $type_name;
} // EndIf it was in list
else {
echo 'Uploaded file type not found';
} // EndElse didn't find it


7:16 pm on Apr 20, 2007 (gmt 0)

10+ Year Member

Thank you very much, that worked perfectly. It could have taken me days to figure that out.

Thanks again.


Featured Threads

Hot Threads This Week

Hot Threads This Month