Reputation: 523
I followed a few sites and have come to this:
<?php
$imagesDir = base_url() . 'images/eventGallery/';
$files = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
for ($i = 0; $i < count($files); $i++) {
$num = $files[$i];
echo '<img src="'.$num.'" alt="random image">'." ";
}
?>
It is not working as nothing is displaying! What am I doing wrong? There are two images in said directory, with ,jpg extension.
Thanks for any assistance!
Upvotes: 0
Views: 4004
Reputation: 191
i would probably do something like this:
foreach (glob('../_pics/about/'.'*.{jpg,jpeg,JPG,JPEG}', GLOB_BRACE) as $filename)
{
echo"$filename";
}
Upvotes: 0
Reputation: 1705
This is working version of your code (altough I would rather use readdir :) ):
define('BASE_URL', dirname(__FILE__));
$imagesDir = BASE_URL . '/images/eventGallery/';
$files = glob($imagesDir . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
$len=count($files);
for ($i = 0; $i < $len; $i++)
{
$num = $files[$i];
// transform from file system path to web path - assuming images is in the web root
$num = substr($num, strlen(BASE_URL) );
echo '<img src="'.$num.'" alt="random image">'." ";
}
Upvotes: 3
Reputation: 563
Just use PHP's built in readdir function
Reference: http://php.net/manual/en/function.readdir.php
<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
?>
Upvotes: 2