我有一个脚本通过一个有3个图像的目录
$imglist='';
$img_folder = "path to my image";
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, checks if are images and ads them to a list
while ($file = $imgs->read()) {
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
$imglist .= "$file ";
}
closedir($imgs->handle);
//put all images into an array
$imglist = explode(" ", $imglist);
//display image
foreach($imglist as $image) {
echo '<img src="'.$img_folder.$image.'">';
}
Run Code Online (Sandbox Code Playgroud)
但我遇到的问题是显示没有图像的第4个img ..但我在该文件夹中只有3个图像.
无需构建一串图像,然后将该字符串分解为图像数组,而只需将图像直接添加到数组中,如Radu所述.这是更正后的代码:
$imglist = array();
$img_folder = "path to my image";
//use the directory class
$imgs = dir($img_folder);
//read all files from the directory, checks if are images and adds them to a list
while ($file = $imgs->read()) {
if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file)){
$imglist[] = $file;
}
}
closedir($imgs->handle);
//display image
foreach($imglist as $image) {
echo '<img src="'.$img_folder.$image.'">';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3017 次 |
| 最近记录: |