PHP and HTML: how to navigate a directory with images?

very simple folder structure like this ...

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

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

Therefore, I do not want to manually specify file names. I just want to add images to a folder and skip php script and create such navigation

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

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

Is it hard to build? Any ideas on this? Thank you in advance for the tips and help.

+6
source share
1 answer

Consider the following: I assume that the img folder is in the / 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; 

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

+10
source

Source: https://habr.com/ru/post/921021/


All Articles