bone282 commented on a Page, Random Image   -  Mar 26, 2010

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)]; }
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.