I am trying to display random images from a directory, but glob
keeps giving me this error:
Undefined offset: 0
Which leads me to believe that it is not matching the .png
files. However shouldn't it match all files in the directory? Thanks
Code:
$banners = glob("/lib/theme/ivao/images/banners/*");
$img1 = $banners[rand(0,count($banners)-1)];
$banners
is empty. So when you try to access an item, PHP returns an error.
To avoid this kinds of mistakes, do not hesitate to show your array with var_dump
or print_r
for example:
$banners = glob("/lib/theme/ivao/images/banners/*");
var_dump($banners);
Several reasons may explain why glob
returns an empty array: PHP can perhaps not access to the directory or it may be empty or does not exist.