this should meet what's required
and can be quickly edited to handle a $fileType array
// Put all files of a directory of A selected filetype into an array.
function dir2array($targetDir, $fileType){
$filesArray = array();
if ($handle = opendir($targetDir)){
while (false !== ($file = readdir($handle))){
if(end(explode(".", $file)) == $fileType){
$filesArray[] = $file;
}
}
closedir($handle);
}
if (count($filesArray) > 0) {
return $filesArray;
} else {
return FALSE;
}
}
// Usage examples:
$arrayName = dir2array("path/to/directory", "jpg");
$randfile = $arrayName[array_rand($arrayName)];
if(FALSE !== ($arrayName = dir2array("path/to/directory", "png"))){ $randfile = $arrayName[array_rand($arrayName)]; }