Welcome to WebmasterWorld Guest from 35.171.183.163

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)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:88
votes: 0


Hi,
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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


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) {
if(in_array($extension,$type_array)){
$found = true;
break;
} // 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)

Junior Member

10+ Year Member

joined:Dec 7, 2004
posts:88
votes: 0


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

Thanks again.