I've seen this php code to get randomized images from a folder. The idea is really good, but we can do it better. I don't like the solution, to send headers with the randomized image, but first step, a little code cleanUp.
Works with php 5.3+
PHP Code:
<?php
$images = array();
try {
foreach (new FilesystemIterator(dirname('images/YOURFOLDER/*')) as $file) {
if (!preg_match('#^(.*)\.(gif|png|jpg)$#i', $file)) {
continue;
}
$images[] = $file->getPathname();
}
}
catch (Exception $e) {
// do something like -> header('Location: images/background/alternativeImage.jpg');
}
$image = mt_rand(0, count($images) - 1);
header('Location: ' . $images[$image]);
Don't use
to close the php code! We don't need it...
Cheers