PHP&;HTML:如何在带有图像的目录中循环


PHP & HTML: how to loop through a directory with images?

像这样非常简单的文件夹结构…

  • index.php
  • img
    • someimage1.jpg
    • someimage2.png
    • someimage3.jpg

我想知道使用php读取这个img文件夹并创建一个微型网站,通过"prev"answers"next"链接循环浏览这些图像有多难。

所以我不想手动指定图像的文件名。我只想将图像添加到文件夹中,并通过php脚本运行它们,创建类似的导航

<a href="nextimage-link" class="next">Next Image</a>
<a href="previmage-link" class="prev">Previous Image</a>

所以每当我点击"下一张图片"链接时,它就会更新网站并显示下一张照片。

建造起来复杂吗?对此有什么想法吗?提前感谢您的小费和帮助。

考虑以下内容,我假设img文件夹在/var/www中:

$dir = "/var/www/img/*.jpg";
//get the list of all files with .jpg extension in the directory and safe it in an array named $images
$images = glob( $dir );
//extract only the name of the file without the extension and save in an array named $find
foreach( $images as $image ):
    echo "<img src='" . $image . "' />";
endforeach;

为了获得更好的UI,您可以创建一个图像路径数组,并将它们存储在JavaScript数组中。然后使用"上一个"answers"下一个"按钮更改图像数组的索引,并在单个img标记中显示当前值。