Random Images

Found this nice little php snippet lying around in my misc script folder. Its relatively easy to install and use. I don’t know the author of the script, so if you know, give me a whisper and I will add the credits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
< ?php
//make this the relative path to the images, like "../img" or "random/images/".
//if the images are in the same directory, leave it blank.
$folder = '../images/random/';
//space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';
$files = array(); $i = -1;
if('' == $folder) {
$folder = './';
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
//for each extension check the extension
foreach($exts as $ext) {
//faster than ereg, case insensitive
if (preg_match('/\.'.$ext.'$/i', $file, $test)) {
$files[] = $file;
++$i;
}
}
}
}
closedir($handle);
mt_srand((double)microtime()*1000000);
$rand = mt_rand(0, $i);
header('Location: '.$folder.$files[$rand]);
?>
Jul 20th, 2006 | Posted in PHP
No comments yet.

Leave a comment

XHTML: Usable tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Disclaimer: For any content that you post, you hereby grant to Deadhouse Gates the royalty-free, irrevocable, perpetual, exclusive and fully sublicensable license to use, reproduce, modify, adapt, publish, translate, create derivative works from, distribute, perform and display such content in whole or in part, world-wide and to incorporate it in other works, in any form, media or technology now known or later developed. Some rights reserved.