phpdirectory

Check for files with 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.


Solution

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