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

  • 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>
    

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

    build 起来很复杂吗?有什么想法吗?
    预先感谢您的小费和帮助。

    最佳答案

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

    关于php - PHP和HTML : how to loop through a directory with images?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11591851/

    10-12 12:50
    查看更多