0

Possible Duplicate:
Get the Files inside a directory

Is there a function that can be used to get the contents of a directory (a photo gallery directory for example) ?

I'm trying to save time on a project by automating a photo gallery based on which files are available.

Thanks

Shane

Community
  • 1
  • 1
shane
  • 179
  • 1
  • 1
  • 8

3 Answers3

2

You can either use the DirectoryIterator:

$dir = new DirectoryIterator('path/to/images');
foreach ($dir as $fileinfo) {
    echo $fileinfo->getFilename() . "\n";
}

or alternatively glob():

$filenames = glob('path/to/images/*.jpg');
foreach ($filenames as $filename) {
    echo $filename ."\n";
}
DASPRiD
  • 1,673
  • 11
  • 16
0

glob()

scandir()

Quasipickle
  • 4,383
  • 1
  • 31
  • 53
  • unless you provide some text with that answer, I really think you should put those as comments only – Gordon Aug 26 '10 at 20:43
  • 1
    I respectfully disagree. He asked for a function - I gave him 2. I think semantically what I wrote is more an answer to his question than a comment about it. – Quasipickle Aug 31 '10 at 19:48
0

I use a while loop to grab a list of files, omit the 2nd if statement if you want to grab a all files.

if ($handle = opendir('/photos/')) {
  while(false !== ($sFile = readdir($handle))) {
     if (strrpos($sFile, ".jpg") === strlen($sFile)-strlen(".jpg")) {
        $fileList[] = $sfile;
     }
  }
}
Mikey1980
  • 971
  • 4
  • 15
  • 24