Quote:
$ext = strstr ($file, "."); // get the file extension
|
You gotta be careful with this, some people like adding extra .'s to their filenames (god knows why), so if someone had a file called my.pic.jpg, $ext would be pic.jpg, which wont work in comparison with the others.
Quote:
while (($file = readdir ($handle)) !== false)
|
I'm not sure why that was changed, i used what was recommended @ php.net as the 'right' way to do it.
Mine was designed to be more customisable, rather than 'just work', so you could select the folder easily, and simple add , "ext" to add an extention to the list..
What i've done here, is taken the speed of yours, and the customisability of mine, and combined them, and got the following :p
PHP Code:
<?php
$target = getcwd() . "/images";
$extentions = Array("PNG", "JPG", "GIF");
$files = Array();
$file_count = 0;
$handle = opendir($target) or die ();
while (($file = readdir($handle)) !== false) {
if (is_dir ($file))
continue;
$tmp = explode(".", $file);
$ext = $file[count($file) - 1];
unset($tmp);
foreach ($extentions as $m00) {
if (strcasecmp($ext, $m00)) {
$files[$file_count++] = $file;
continue 2;
}
}
}
closedir ($handle);
if (!$file_count)
return;
readfile ($files[rand (0, $file_count) -1]);
?>
Its not commented, but you should know what it does by now