terungwa Posted July 15, 2014 Share Posted July 15, 2014 My images generator comprises an array of image names extracted form an images table from a database using a select statement a random number generator, 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. Link to comment https://forums.phpfreaks.com/topic/289915-random-image-generator-warning-getimagesizeimages-failed-to-open-stream-no-such-file-or-directory-in/ Share on other sites More sharing options...
Ch0cu3r Posted July 15, 2014 Share Posted July 15, 2014 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); } Link to comment https://forums.phpfreaks.com/topic/289915-random-image-generator-warning-getimagesizeimages-failed-to-open-stream-no-such-file-or-directory-in/#findComment-1485309 Share on other sites More sharing options...
terungwa Posted July 15, 2014 Author Share Posted July 15, 2014 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? Link to comment https://forums.phpfreaks.com/topic/289915-random-image-generator-warning-getimagesizeimages-failed-to-open-stream-no-such-file-or-directory-in/#findComment-1485313 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.