Connecting Tech Pros Worldwide Help | Site Map

Choose random image from dir and display it

  #1  
Old July 17th, 2005, 10:22 AM
kingofkolt
Guest
 
Posts: n/a
I have a directory of images, called "random". In it are the following
files:

1.gif
2.gif
3.gif
4.gif

I use this script to choose a random image and display it:

$i = -2; // $i = -2 so that the directory files ("." and "..") won't be
counted.
if ( $dh = opendir( "images/random" ) ) {
while ( false !== ( $file = readdir( $dh ) ) ) {
$i++;
}
closedir( $dh );
$rand = rand( 1, $i );
$randimg = '<img src="images/random/' . $rand . '.gif" />';
}
print $randimg;

This works just dandy. However, I figure there must be a more efficient way
to count the number of files in a directory. Is there a directory's
equivalent of a function like file()? Or is my script the only way to count
the files?

Thanks.
- JP


  #2  
Old July 17th, 2005, 10:22 AM
neur0maniak
Guest
 
Posts: n/a

re: Choose random image from dir and display it


You probably find it safer to keep doing it the way you are.

Or instead modify it to build an array containing filenames and then
randomly pick an entry from the array. This is to account for if a file
is ever removed, no errors would occur.


kingofkolt wrote:[color=blue]
> I have a directory of images, called "random". In it are the following
> files:
>
> 1.gif
> 2.gif
> 3.gif
> 4.gif
>
> I use this script to choose a random image and display it:
>
> $i = -2; // $i = -2 so that the directory files ("." and "..") won't be
> counted.
> if ( $dh = opendir( "images/random" ) ) {
> while ( false !== ( $file = readdir( $dh ) ) ) {
> $i++;
> }
> closedir( $dh );
> $rand = rand( 1, $i );
> $randimg = '<img src="images/random/' . $rand . '.gif" />';
> }
> print $randimg;
>
> This works just dandy. However, I figure there must be a more efficient way
> to count the number of files in a directory. Is there a directory's
> equivalent of a function like file()? Or is my script the only way to count
> the files?
>
> Thanks.
> - JP
>
>[/color]
  #3  
Old July 17th, 2005, 10:22 AM
Theo
Guest
 
Posts: n/a

re: Choose random image from dir and display it


neur0maniak <usenet@neur0maniak.co.uk> wrote in news:41830267$0$43631
$ed2e19e4@ptn-nntp-reader04.plus.net:
[color=blue]
> You probably find it safer to keep doing it the way you are.
>
> Or instead modify it to build an array containing filenames and then
> randomly pick an entry from the array. This is to account for if a file
> is ever removed, no errors would occur.
>[/color]

Try this, as described above. Forget where I found the code but it works
just fine. $dirpath should be a fullpath to the appropriate directory and
requires a slash at the end... or you can add it to the couple of lines
in between the path and the image filename. Everything in the directory
except those in the if statement will be put into the array.

$dir_contents = array();
$d = dir($dirpath);
while ( $crnt_file = $d->read() ) {
if ($crnt_file == "." || $crnt_file == ".."
|| is_dir($dirpath.crnt_file))
continue;
$dir_contents[] = $dirpath.$crnt_file;
}
$d->close();
  #4  
Old July 17th, 2005, 10:22 AM
Fox
Guest
 
Posts: n/a

re: Choose random image from dir and display it


kingofkolt wrote:[color=blue]
> I have a directory of images, called "random". In it are the following
> files:
>
> 1.gif
> 2.gif
> 3.gif
> 4.gif
>
> I use this script to choose a random image and display it:
>
> $i = -2; // $i = -2 so that the directory files ("." and "..") won't be
> counted.
> if ( $dh = opendir( "images/random" ) ) {
> while ( false !== ( $file = readdir( $dh ) ) ) {
> $i++;
> }
> closedir( $dh );
> $rand = rand( 1, $i );
> $randimg = '<img src="images/random/' . $rand . '.gif" />';
> }
> print $randimg;
>
> This works just dandy. However, I figure there must be a more efficient way
> to count the number of files in a directory. Is there a directory's
> equivalent of a function like file()? Or is my script the only way to count
> the files?
>
> Thanks.
> - JP
>
>[/color]

php 4.3 or greater:

$imgGroup = glob("images/random/{*.jpg,*.gif,*.png}", GLOB_BRACE);

echo "
<img src = {$imgGroup[rand(0, count($imgGroup)-1)]} />
";

Fox
**************



  #5  
Old July 17th, 2005, 10:23 AM
Chung Leong
Guest
 
Posts: n/a

re: Choose random image from dir and display it


"kingofkolt" <jessepNOSPAM@comcast.net> wrote in message news:<WMBgd.340547$D%.310394@attbi_s51>...[color=blue]
> I have a directory of images, called "random". In it are the following
> files:
>
> 1.gif
> 2.gif
> 3.gif
> 4.gif
>
> I use this script to choose a random image and display it:
>
> $i = -2; // $i = -2 so that the directory files ("." and "..") won't be
> counted.
> if ( $dh = opendir( "images/random" ) ) {
> while ( false !== ( $file = readdir( $dh ) ) ) {
> $i++;
> }
> closedir( $dh );
> $rand = rand( 1, $i );
> $randimg = '<img src="images/random/' . $rand . '.gif" />';
> }
> print $randimg;
>
> This works just dandy. However, I figure there must be a more efficient way
> to count the number of files in a directory. Is there a directory's
> equivalent of a function like file()? Or is my script the only way to count
> the files?
>
> Thanks.
> - JP[/color]

Yes. You can do all that in a couple lines:

$file = array_rand(glob("images/random/*.gif"));
echo "<img src=\"$file\">";
Closed Thread