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?

Archived

This topic is now archived and is closed to further replies.

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