【问题标题】:PHP & HTML: how to loop through a directory with images?PHP & HTML:如何循环浏览带有图像的目录?
【发布时间】:2012-07-20 11:14:08
【问题描述】:

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

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

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

所以我不想手动指定图像的文件名是什么。我只想将图像添加到文件夹中,然后运行一个 php 脚本并创建这样的导航

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

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

构建起来很复杂吗?有什么想法吗? 提前感谢您的提示和帮助。

【问题讨论】:

标签: php html directory


【解决方案1】:

考虑以下情况,我假设 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 数组中。然后使用 Previous 和 Next 按钮更改图像数组的索引,并在单个 img 标记中显示当前。

【讨论】:

  • 我喜欢关于 JS 数组的想法。如何用php创建一个JS数组?
  • 可以使用realpath() PHP函数获取实际路径。
  • 抱歉,但有注释只提取不带扩展名的文件名并保存在名为 $find 的数组中 我真的找不到将结果保存到 @ 的位置987654324@数组
猜你喜欢
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
相关资源
最近更新 更多