Jump to content


Photo

find all files in a folder, choose one randomly


  • Please log in to reply
7 replies to this topic

#1 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 24 July 2006 - 05:24 PM

I'm working on a random background image selector... I'd like it to check the appropriate folder for all files (there would only be jpegs) and choose one filename and make it a variable. How is this accomplished?
http://www.DaveLinger.com
dave at linger dot com

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 24 July 2006 - 05:34 PM

create a list of files, then count the lines, then feed that into a random number gen then display the line that equals the results

#3 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 24 July 2006 - 05:35 PM

well I mean I know how to randomly select from an array, just not how to find all files in a folder and put them into said array.
http://www.DaveLinger.com
dave at linger dot com

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 24 July 2006 - 05:39 PM

http://us2.php.net/m.../en/ref.dir.php
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 24 July 2006 - 05:49 PM

okay, it says that

<?php
$dir = "/images/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
           echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
       }
       closedir($dh);
   }
}
?>

should display any files in the "images" directory, but echos nothing. script is located...

domain
    www
          index.php
          test.php
          script.php
          images/
                image.jpg
                image2.jpg
                image3.jpg
http://www.DaveLinger.com
dave at linger dot com

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 July 2006 - 06:07 PM

You may also want to look at the glob() function (http://www.php.net/glob) which returns all the file names in directory as an array. Then use array_rand() to select one at random.

<?php
$dir = glob("images/*.jpg");
$rand_file = $dir[array_rand($dir)];
echo $rand_file;
?>

Ken

#7 DaveLinger

DaveLinger
  • Members
  • PipPipPip
  • Advanced Member
  • 268 posts
  • LocationWV, USA

Posted 24 July 2006 - 06:53 PM

oh skeet

thanks
http://www.DaveLinger.com
dave at linger dot com

#8 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 24 July 2006 - 06:58 PM

You should look into scandir() too :)

Orio.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users