Needs more getimagesize() and less munging around with strings and regular expressions trying to infer file type.
>My code, of course, is flawless.
$it=array_rand($files);
header("Location: ".$url.$file);
Eh? $file is the last file you read in the readdir() loop, no?