6

very simple folderstructure 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 loops through this images with "prev" and "next" links.

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.

2
  • glob() Commented Jul 21, 2012 at 11:41
  • Or readdir Commented Jul 21, 2012 at 11:54

1 Answer 1

12

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;

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I love the idea about the JS Array. How can I create a JS array with php?
You can use the realpath() PHP function to get the actual path.
Sorry but with comment extract only the name of the file without the extension and save in an array named $find I can't really find where you save the result into $find array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.