使用PHP检查文件[重复]

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

You can either use the DirectoryIterator:

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

or alternatively glob():

$filenames = glob('path/to/images/*.jpg');
foreach ($filenames as $filename) {
    echo $filename ."
";
}

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;
     }
  }
}