Jump to content

Get contents of a directory of images into an array :-|


TGWSE_GY

Recommended Posts

I am needing to know if I have a directory called 'images' how I would recursively just load the file names 'imagename.jpg' into an array of key => values and the keys being incremental integers. I know this should probably be easy but as I am just learning about directories and how to interact with them.

 

Any help would be great!

 

Thanks Guys  :D

I appreciate the help Ken, but as I am completley unsure of what is going to be stored in the directory at all times I guess it is my lack in understanding directories to arrays that makes glob() seem un-helpful. Can you give me an example or another method that may help me better understand the process.

 

Thanks

George!  8)

The best way to see what the function does is to create a small script and run it:

<?php
$files = glob('path/to/your/images/*.jpg');
echo '<pre>' . print_r($files,true) . '</pre>'; // this will dump the array to the screen so you can see what's in it
foreach ($files as $fn) { // loop through the array to get each file
    echo '<a href="' . $fn . '">' . $fn . "</a><br>\n";  // put a link to each file on the page
}
?>

 

Put this script on your server and when run, it will give you a list of the images as links. Click on each link to see the picture.

 

Ken

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.