PHP- List All Files In A Directory

PHP Scripts, Tips & Tricks

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

//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.


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.


  1. Conor spoke on 19/02/2009

    Thanks that example helped me a lot!

  2. cstulo spoke on 07/03/2009

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

  3. Mehedi Hasan spoke on 05/08/2009

    Very helpful post… Thanks.

    Mehedi Hasan

  4. Rickard Sogge spoke on 27/08/2009

    Great code, to get better support use

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


  5. James spoke on 12/10/2009

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


  6. Matt spoke on 23/01/2010

    How about listing files by date modified?

  7. satheesh spoke on 02/03/2010

    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.

  8. Victor Campusano spoke on 30/03/2010

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

  9. Matt spoke on 09/07/2010

    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?



  10. sam spoke on 18/08/2010

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

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

  11. Bofy spoke on 27/04/2011

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

  12. Murray spoke on 10/05/2011

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

  13. Daithi O Broin spoke on 11/05/2011

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

  14. Mike J spoke on 17/06/2011

    Thank you very much! It worked instantly!

  15. desbest spoke on 17/07/2011

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

  16. Maruf spoke on 20/07/2011

    Hey desbest,

    Try this blog post, List All Directories/Folders From A Specified Location

  17. Terran spoke on 05/08/2011

    Great tip! Works like a charm.

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

  18. simon spoke on 17/08/2011

    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().

  19. adm1n spoke on 17/08/2011

    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.

  20. desmond spoke on 02/09/2011

    I have this error when using the code.

    Warning: Invalid argument supplied for foreach() in

    Please help

  21. Kris spoke on 27/09/2011

    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 spoke on 04/10/2011

    such a beautiful solution!

  23. dan spoke on 22/10/2011

    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?


  24. Joshua Purcell (08JPurcell) spoke on 03/11/2011

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

    for me ๐Ÿ™

  25. Joshua Purcell (08JPurcell) spoke on 03/11/2011

    Actually I just realised I made a Typo in the code, works now ๐Ÿ™‚ THANKS!!!!!!

  26. Mike spoke on 08/11/2011

    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 spoke on 08/11/2011

    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 spoke on 21/12/2011

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

  29. Otesto spoke on 07/01/2012

    great code. any ideas to make it search subdirectories

  30. Shakeel spoke on 17/02/2012

    Thanks Works Great ๐Ÿ™‚

  31. Helen Neely spoke on 17/02/2012

    Nice and short solution to a very common requirement.
    thanks for the code.

  32. Darren Azzopardi spoke on 16/03/2012

    Thanks for the code, really helpful. How would i go about retrieving only 5 images from the directory rather than everything..

    Would I need to count the images first and then proceed with your code?

    Any help would be great..


  33. randy spoke on 04/04/2012

    Awesome tut. Thanks! For anyone who is interested, you can make those files links very easily by changing

    echo $image;


    echo ‘‘.$image.’‘;

  34. inwing spoke on 02/07/2012

    How about listed by date modified?

  35. Wladass spoke on 16/08/2012

    Easy and simple, thanks !

  36. Joseph spoke on 22/09/2012

    good one

  37. wayne spoke on 09/10/2012

    and now I got it so that it shows the files over and over a whole bunch of times lol…I don’t know y i’m having so much trouble with this

  38. Zaf spoke on 30/11/2012

    it must be opendir() before passing into the glob() function

  39. Ryan spoke on 21/12/2012

    Thank you! It worked like a charm. A very neat post.nn1

  40. Lokesh spoke on 24/01/2013

    Thanks, It worked.

  41. PJ spoke on 18/04/2013

    Saw so mant examples of this with lines and lines of code, splitting arrays, joining arrays and this does it in a few lines… Thank you for saving me a few hours of my life.

  42. Claudio Kuenzler spoke on 03/05/2016

    Thanks! One of the simplest yet working PHP snippet to simply list files. All the others (higher ranked in search results) snippets were more complicated and didn’t work correctly.

Leave a reply

Your email address will not be published. Required fields are marked *