selecting random files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bigeasy
Forum Newbie
Posts: 2
Joined: Tue Sep 18, 2007 8:32 am

selecting random files

Post 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?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

glob() and array_rand() maybe?
bigeasy
Forum Newbie
Posts: 2
Joined: Tue Sep 18, 2007 8:32 am

Post 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!
nhammond
Forum Newbie
Posts: 14
Joined: Mon Dec 18, 2006 11:35 am

Post 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
Post Reply