Web Design Blog

This is where we store some of our Web Development thoughts, tips and tricks, just because we like to share.

PHP- List All Files In A Directory

I’m working on a website for a client at the moment, and one of the features I need to do is display a bunch of images in a particular directory.

It’s actually a very simple process in PHP and only requires a small amount of code. I’ve seen a lot of examples on other forums/websites that use a ridiculous amount of code to get the same result, and i’m not quite sure why.

Here’s how to list all files in a directory

1
2
3
4
5
6
7
8
9
10
11
//path to directory to scan
$directory = "../images/team/harry/";
 
//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");
 
//print each file name
foreach($images as $image)
{
echo $image;
}

It’s as easy as that.

Notes

The .jpg extension can be changed to any extension. So if you want to pull out only .txt files, you just need to replace the .jpg with .txt. If you want to list ALL files, just remove the condition.

02 Nov 2008 / 29 Comments / PHP Scripts, Tips & Tricks / by Maruf

29 Comments

  1. Conor
    19/02/2009
    1

    Thanks that example helped me a lot!

  2. cstulo
    07/03/2009
    2

    Simple and powerful. Thanks, it has helped me too!

  3. Mehedi Hasan
    05/08/2009
    3

    Very helpful post… Thanks.

    Mehedi Hasan

  4. Ralph
    27/08/2009
    4

    One of the best posts I’ve seen for some time, thanks a lot.

  5. Rickard Sogge
    27/08/2009
    5

    Great code, to get better support use

    $images = glob(“” . $directory . “{*.jpg,*.gif,*.png}”, GLOB_BRACE);

    Thanks!

  6. James
    12/10/2009
    6

    Code at its best – short, sweet and easy to understand!

    Thanks

  7. Matt
    23/01/2010
    7

    How about listing files by date modified?

  8. satheesh
    02/03/2010
    8

    It solved my problem.

    Thank you.

    Is it possible to get the absolute path of a file which is located in any location of the system using php.

  9. Victor Campusano
    30/03/2010
    9

    So Helpful. Thanks a Lot. It helped me to show a list of news in flat files.

  10. Matt
    09/07/2010
    10

    Hi mate, great script.. thank you! Just wondering, how would I go about displaying the images in order? For example if they were named 1.jpg – 10.jpg?

    Thanks

    Matt

  11. sam
    18/08/2010
    11

    hey everyone, here’s a function to output certain image types, with the option to put its name under the image. -sam

    function list_images($folder,$type,$listnames){

    $ext = ($type === ‘all’) ? ‘{*.jpg,*.gif,*.png}’ : ‘*.’.$type;
    $files = glob($folder.$ext, GLOB_BRACE);
    sort($files);

    foreach ($files as $file) {
    if ($file “.” && $file “..” && !preg_match(“/^hide/i”,$file)){
    echo ”;
    echo $listnames ? ”.str_replace($folder, ”, $file).” : ”;
    }
    }
    }

    say I want to list all jpg with name underneath—

    list_images(‘./’,'jpg’, true);

    or if I want to list all images with no name output–

    list_images(‘./’,'all’, false);

  12. 27/04/2011
    12

    Your code works perfectly.
    I’ve tried a lot of code snippets from other websites, more complicated but nothing good.
    Thank you!

  13. Murray
    10/05/2011
    13

    Wow, so simple. Thanks for bringing ‘glob’ to my attention!

  14. Daithi O Broin
    11/05/2011
    14

    So simple and so good.
    My query – how would I make the list into downloadable links/ Would this work for .doc/.pdf files also?

  15. Mike J
    17/06/2011
    15

    Thank you very much! It worked instantly!

  16. desbest
    17/07/2011
    16

    How can the code snippet be modified, so that I can only list folders, or only list files?

  17. Maruf
    20/07/2011
    17
  18. 05/08/2011
    18

    Great tip! Works like a charm.

    Also thanks to Rickard Sogge (above) for showing how to add gif and png support.

  19. simon
    17/08/2011
    19

    Does anyone know how to do this for a directory located on a different server? Say you have your images located elsewhere, or just want to list all files using a path to any arbitrary domain.

    This should be managable, especially if the directory listing is open when visited directly with the borwser. However it seems all paths that are not local/relative are failed by is_dir().

  20. adm1n
    17/08/2011
    20

    Hi Simon,

    What makes you think that’s possible? I can’t imagine it would be because it would be a massive security risk- then anyone would be able to access all your files.

  21. 27/09/2011
    21

    Hey hey, great script! Thanks!

    Is there a way to put .txt files and its content in there too? I mean having both jpgs and txts sorted by name and displayed?

    Thanks for the help!

  22. endofline
    04/10/2011
    22

    such a beautiful solution!

  23. dan
    22/10/2011
    23

    Great script. I can get it to work fine anywhere on my server, except when i try to use it as part of a wordpress theme. Does anybody know why this is?

    Thanks

  24. 24

    Seems to say
    Warning: Invalid argument supplied for foreach() in /home/jpforum/public_html/Echo_Dir.php on line 17

    for me :(

  25. 25

    Actually I just realised I made a Typo in the code, works now :) THANKS!!!!!!

  26. Mike
    08/11/2011
    26

    Great piece of code! This works great for me, I changed .jpg to .html. The only thing I need help with is removing the _ and .html portion of the filename.

    In my echo I have:

    echo “$name“;

    How can I have $link strip out the _ and .html portion and have $name just display the filename? Thanks!

  27. adm1n
    08/11/2011
    27

    Mike, you can use the “str_replace” function.

    So before you echo $name, you can do the following:

    $name = str_replace(“.html”,”",str_replace(“_”,”",$name));

  28. John
    21/12/2011
    28

    Any idea about how this could be done recursively so it can check all the subdirectories too?

  29. Otesto
    07/01/2012
    29

    great code. any ideas to make it search subdirectories

Leave a Reply

© 2012 BrightCherry :)