随机图像生成器 - 警告:getimagesize(images /):无法打开流:没有这样的文件或目录

My images generator comprises

  1. an array of image names extracted form an images table from a database using a select statement
  2. a random number generator,
  3. and a string that builds the correct pathname for the selected file.

To select one of the images for display, i generate a random number between 1 and the length of the array.

Though the generator is working, I noticed one of the random numbers is throwing up this error:

Warning: getimagesize(images/): failed to open stream: No such file or directory in

An inspection of the array reveals 2 array elements (representing my number of images) but one array element is NULL ( the first entry in the banner table

IMAGE GENERATOR SCRIPT

    require_once('connection.inc.php');
    $sql = 'SELECT `filename` FROM  banner';
    $result = $mysqli->query($sql, MYSQLI_STORE_RESULT) or die(mysqli_error());
    $row = $result->fetch_array(MYSQLI_ASSOC);//an array of image names
    $count = $result->num_rows;
    for ($i = 1; $i <= $count; ++$i) 
    {
       $row[$i] = $result->fetch_array(MYSQLI_ASSOC);
    }
    $i = rand(1, $count); //a random number generator,
       //The random number is used in the final line to build the correct pathname for the selected file.
       $selectedImage = "images/{$row[$i]['filename']}"; 
      if (file_exists($selectedImage) && is_readable($selectedImage)) 
      {
        $imageSize = getimagesize($selectedImage);
      }

var_dump($row)

    array (size=1)
    'filename' => string 'ginsomin2.jpg' (length=13)
    null

RANDON IMAGE DISPLAY

   <div id="banner" class="wrapper clearfix">
      <img src="<?php echo $selectedImage; ?>" alt="banner">
   </div>

Any ideas on how i may proceed from here?

Thanks.

I used ORDER BY RAND() to chose a random filename. This combined with LIMIT is useful for selecting a random sample from a set of rows. The list() function was used to assign a random value (image filename) from the set of result rows in the image table to the variable ($bannerImage ).

   $sql = 'SELECT `filename` FROM  banner ORDER BY RAND() LIMIT 1';

   $result = $mysqli->query($sql) or die(mysqli_error());
   $ok =$result->fetch_row();
   list($chosenFilename) = $ok;

   $bannerImage = "images/$chosenFilename"; 
   if (file_exists($selectedImage) && is_readable($selectedImage)) 
    {
       $imageSize = getimagesize($selectedImage);
    }

The $bannerImage used thus

    <div id="banner">
       <img src="<?php echo $bannerImage ; ?>" alt="banner">
    </div> 

outputs the desired result.