tags:

views:

39

answers:

4

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

+2  A: 

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 glob() eh? awesome my friend!
Mikey1980
I've opted for glob(). Perfect! Thank you.
shane
@shane note that as of PHP5.3, there is also a [`GlobIterator`](http://de.php.net/manual/en/class.globiterator.php)
Gordon
A: 

glob()

scandir()

Pickle
unless you provide some text with that answer, I really think you should put those as comments only
Gordon
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.
Pickle
A: 

Have a look at:

Tutorial about reading contents of directory / folder with php

Sarfraz
A: 

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