Skip to main content
October 27, 2006
Question

PHP Random Images Function

  • October 27, 2006
  • 3 replies
  • 302 views
I had made a php random image function and the problem I have at this moment is I don't want to change the "4" number to a "5" because I included another image. Is there a value where the "4" is some php function so I do not need to always update?

<?php
$roll = rand(1,4);
print "<img src = randImages/Bn$roll.jpg>";
?>

Thank you,
AdonaiEchad
This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

October 31, 2006
Thank you for all of your help it worked.
October 31, 2006
Thank you for all of your help.
Inspiring
October 28, 2006
.oO(AdonaiEchad)

>I had made a php random image function and the problem I have at this moment is
>I don't want to change the "4" number to a "5" because I included another
>image. Is there a value where the "4" is some php function so I do not need to
>always update?
>
> <?php
> $roll = rand(1,4);
> print "<img src = randImages/Bn$roll.jpg>";
> ?>

You could try something like this:

<?php
$path = $_SERVER['DOCUMENT_ROOT'].'/randImages';
$roll = rand(1, count(glob("$path/*.jpg"));
print "<img src='/randImages/Bn$roll.jpg' alt='...'>\n";
?>

glob() searches a given directory for filenames matching the wildcard
pattern (in this case *.jpg). It returns an array with all found
filenames. count() then simply returns the array's size and passes it to
rand().

HTH
Micha