Jump to content

Load Random Images from a directory..


gacon

Recommended Posts

Please let me know what happen to this code, it works, but sometimes it does not when I refresh the page.
Thank you very much.

<?
function fun($Directory)
{
   $dir = opendir($Directory);
   $index = 0;
   while($imgfile=readdir($dir))
   {
      if ($imgfile != "." && $imgfile!="..")
      {
         $imgarray[$index]=$imgfile;
         $index++;
      }
   }
   closedir($dir);
   $rand = rand(0,count($imgarray)-1);
   if($rand >= 0)
   {
     echo '<CENTER><img src="c/div/'.$imgarray[$rand].'"width="10%" height="2%">';
   }
}

echo fun("c/div/");

?>

If I change the line below in red, then it does not work at all... I can not figure it out.
Thank you very much.


<?
function fun($Directory)
{
   $dir = opendir($Directory);
   $index = 0;
   while($imgfile=readdir($dir))
   {
      if ($imgfile != "." && $imgfile!="..")
      {
         $imgarray[$index]=$imgfile;
         $index++;
      }
   }
   closedir($dir);
   $rand = rand(0,count($imgarray)-1);
   if($rand >= 0)
   {
     echo '<CENTER><img src="[color=red]$Directory'[/color].$imgarray[$rand].'"width="10%" height="2%">';
   }
}

echo fun("c/div/");

?>
Link to comment
https://forums.phpfreaks.com/topic/27319-load-random-images-from-a-directory/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.