4

I want to simply pull all the JPG files from a specific folder (on MY server) into an array. I figure it would look something like this. My logic here is that I'd have a folder with images I want in a gallery so I could just upload images with an FTP and they would just show up. Is this a good idea?

$dir = 'www.example.com/folder1/';

$images_array = SOMEFUNCTION($dir);

foreach ($images_array) as $v){
echo '<img src="'.$dir.$v.'" />";
}

Thanks for the help!

dhornbein
  • 214
  • 8
  • 19
  • Duplicate: https://stackoverflow.com/questions/4202175/php-script-to-loop-through-all-of-the-files-in-a-directory/37237486#37237486 – Julian Apr 19 '18 at 12:23

2 Answers2

11

glob() would work well here:

$images_array = glob($dir.'*.jpg');

As Zarel commented, you'd have to do a string replacement on the files in the list, as glob() will give you the file path in the system, which won't be a direct URL. Chop off the directory prefix and replace it with a URL prefix using str_replace() when you're outputting links.

zombat
  • 92,731
  • 24
  • 156
  • 164
  • 1
    Keep in mind that you have to use relative paths or internal paths... `$dir = 'www.example.com/anything'` won't work. – Zarel Aug 01 '09 at 23:23
1

Try directory iterator from Standart PHP Library

Nik
  • 9,063
  • 7
  • 66
  • 81