ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

List all files in a directory

With this codesnippet you can very easily list all the files in a directory, effectively creating a web-based filemanager (if you extend it with other functions).

if ($handle opendir('/path/to/files')) {
    echo 
"Directory handle: $handle\n";
    echo 
"Files:\n";

    
// List all the files
    
while (false !== ($file readdir($handle))) {
        echo 
"$file\n";
    }

    
closedir($handle);
}
?> 

5 Responses to “List all files in a directory”

  1. dj Says:

    I’m a novice - i don’t kmow how to customize (I assume the red text) for my site.

  2. Karl Greenhalgh Says:

    An example if you want all of a specific file type in a directory.

    $GalleryDirectory = “albums/”;
    foreach (glob(”$GalleryDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}”, GLOB_BRACE) as $photo)
    {
    echo

    endHTML;
    }

  3. Karl Greenhalgh Says:

    Sorry it edited my code out. in the foreach echo use img src=$photo

  4. Nino Says:

    Doing with foreach is better.

    And the while in this case is way to complicated, with booleans and everything.

  5. Kevin Loring Says:

    IS there any way to create an array of images from a specified directory (Says, /pics/)
    then preload each image from the newly created array? I would also need some sort of progress bar to illuminate progression after which a redirect would be required.

    Any suggestions?

Leave a Reply