Jump to content

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


Go to solution Solved by Ch0cu3r,

Recommended Posts

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.php

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

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

Kindly advice how i may proceed from here?

Thanks.

  • Solution

If you are only chosing a random filename defined in the banner table you could handle this with MySQL.

$sql = 'SELECT `filename` FROM  banner ORDER BY RAND() LIMIT 1'; // randomise the order of the table and return the first row

$result = $mysqli->query($sql) or die(mysqli_error());

list($chosenFilename) = $result->fetch_row();

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

 

If you are only chosing a random filename defined in the banner table you could handle this with MySQL.

$sql = 'SELECT `filename` FROM  banner ORDER BY RAND() LIMIT 1'; // randomise the order of the table and return the first row

$result = $mysqli->query($sql) or die(mysqli_error());

list($chosenFilename) = $result->fetch_row();

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

@Ch0cu3r. Thank you very much. This has worked perfectly. But what was wrong with my query?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.