Topic: random image

iv been lookin for some time for a scrpt that will display an image from any dir under a image/ dir and i just wanted to know if anyone knows how to get a random file form any dir under a certin dir.

maybe some one could sugest a random image script too.

2 (edited by Nibbler(cpg) 2004-08-20 12:35)

Re: random image

<?php

$dir = 'images';

function scan_directory($dir)
{
    global $list;
    
    $dirlist = opendir($dir);
    while (false !== ($file = readdir($dirlist)))
    {
        if ($file != '.' && $file != '..')
        {
            $newpath = $dir.'/'.$file;
            if (is_dir($newpath)){
                scan_directory($newpath);
            } else {
                array_push($list,$newpath);
            }
        }
    }
    closedir($dirlist);
}

$list = array();
scan_directory($dir);
$key = array_rand($list);
header("Location: http://" . $_SERVER['HTTP_HOST'] . "/{$list[$key]}");
exit;
?>

Put that in a file pic.php outside the images directory, and you can use it in an img tag <img src="pic.php"> from any page.

Re: random image

Thanx alot nibbler

4

Re: random image

Thanks for the code, how's the CPG integration going?

Re: random image

It has just been released in Coppermine 1.3.2 available here

Re: random image

im havein proglems with the script, i put it in the img tag and it wont load any image, i look at the image props and its lookin for the image pic.php

Re: random image

Run pic.php in your browser and see what it does.

Re: random image

its finding the file in the dir but the redirect isnt goin to the right folder... its goin down to the root dir and than /images/...

Re: random image

oh i got it, i changed the header location, the img tag is workin now, thanx

10

Re: random image

Nibbler(cpg) wrote:

It has just been released in Coppermine 1.3.2 available here

Thanks, will give it a try.

Could you direct me in the right direction to create a cpg for Nucleus, similar to the slim down versions that are available for other CMS's.

Thanks.