Jump to content

How can I create array based on files in a directory folder?


Conjurer

Recommended Posts

I have a folder that contains some various images in jpg, gif, etc format. I want to load the file names into an array so that I can then use them in a random generator to change images when the page refreshes.

 

How would I get the filenames into an array?

 

I have used a script to generate the images randomly as follows:

PHP Code:

<?php
    $picturesArray = array("trip_1.jpg", "trip_2.jpg",
                           "trip_5.jpg", "trip_6.jpg",
                           "trip_8.jpg", "trip_9.jpg",
                           "trip_12.jpg","trip_13.jpg",
                           "trip_14.jpg","trip_15.jpg");
    srand((float) microtime() * 10000000);
    shuffle($picturesArray);
    
    echo "<img src=\"../../assets/images/Deschutes/$picturesArray[0]\"";
    echo " alt=\"Deschutes River, Oregon\" >";
    ?>

 

But with this I have to save all the files as trip_1.jpg, trip_2.jpg, etc.

 

I would like to modify this so I could create the $picturesArray based on the filenames in the directory I want to access, then do the shuffle to mix them up, then echo out the one to show.

 

Any thoughts?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.