PHP& HTML:如何循环通过目录与图像? [英] PHP & HTML: how to loop through a directory with images?

查看:117
本文介绍了PHP& HTML:如何循环通过目录与图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

very simple folderstructure like this …


  • index.php

  • img

    • someimage1.jpg

    • someimage2.png

    • someimage3.jpg

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

      我想知道使用php读取这个img文件夹并创建一个微型站点,通过这个图像循环上一页和下一个链接。

      I wonder how difficult it is to use php to read this img folder and create a microsite that loops through this images with "prev" and "next" links.

      所以我不想手动指定图像的文件名。我只想添加图像到文件夹,一个php脚本贯穿它们,并创建一个这样的导航

      So I don't want to manually specify what the filenames of the images are. I just want to add images to the folder and a php script runs through them and creates a navigation like that

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

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

      So whenever I click on the "Next Image" link it updates the site and shows the next image.

      这样复杂吗?有什么想法吗?
      提前谢谢你的提示和帮助。

      Is that complicated to build? Any ideas on that? Thank you in advance for your tipps and help.

      推荐答案

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

      Consider the following, I am assuming the img folder is in /var/www folder:

      $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 标签中显示当前值。

      For a better UI, you can create an array of image paths and store them in a JavaScript array. Then use the Previous and Next button to change the index of the image array and display the current in the single img tag.

      这篇关于PHP&amp; HTML:如何循环通过目录与图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆