Page 1 of 1

selecting random files

Posted: Tue Sep 18, 2007 9:00 am
by bigeasy
This should be easy for you guys but I don't even know where to start:

I've got a folder that I add and subtract html files from. At any point, when the user submits a form, I want to exit to a random html file in that folder. How do I select the file randomly?

Posted: Tue Sep 18, 2007 9:04 am
by Zoxive
glob() and array_rand() maybe?

Posted: Tue Sep 18, 2007 9:50 am
by bigeasy
Yep. Thx Zox, it woulda taken me hours to dig through the PHP functions before I hit these two. Man, you guys are qwik!

Posted: Tue Sep 18, 2007 4:26 pm
by nhammond

Code: Select all

$dir=opendir($picture_path);

$i=0;
while($imgfile=readdir($dir))
{
     if ($imgfile != "." && $imgfile!="..")
         {
        $imgarray[$i]=$imgfile;
        $i++;
        }
}

closedir($dir);

$rand=rand(0,count($imgarray)-1);

    if($rand >= 0)
    {
    $file = $imgarray[$rand];
}
That script is pretty nifty too