我关注了几个网站并且已经来到这里:
<?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">'." ";
}
?>
它无法正常显示!我究竟做错了什么?所述目录中有两个图像,带有jpg扩展名。
感谢您的帮助!
答案 0 :(得分:3)
这是您的代码的工作版本(尽管我宁愿使用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">'." ";
}
答案 1 :(得分:2)
只需使用PHP内置的readdir函数
参考: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);
}
?>
答案 2 :(得分:0)
我可能会这样做:
foreach (glob('../_pics/about/'.'*.{jpg,jpeg,JPG,JPEG}', GLOB_BRACE) as $filename)
{
echo"$filename";
}