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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

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.