Subscribe to How-To Geek

PHP: Display a customizeable list of files in a directory

Apache gives you a list of files in an empty directory by default, but sometimes you will want to show a list of files that are in a directory through PHP so that you can customize the output of the list, and make a “pretty” listing of files. Here’s the basic code to make a list.

 

<?php
 if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
	  {
          	$thelist .= '<a href="'.$file.'">'.$file.'</a>';
          }
       }
  closedir($handle);
  }
?>
<P>List of files:</p>
<P><?=$thelist?></p>

 

That’s about all you need to do. You could also put the list of files into an array if you felt like it, or use this as the beginning for a thumbnail page, which is what I used it for.

| More
This article was originally written on 09/25/06 Tagged with: PHP, Programming

Daily Email Updates

You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Comments (47)

  1. Oz

    I found that the following line needed a newline entry to make the list more readable:

    $thelist .= ‘‘.$file.’

    and also the last line seems not to work for me the way its coded above. just use echo $thelist; is fine.

    Oz

  2. Newb

    Is there any way to make the list not list the php file generating the list?

    The above code saved as “list.php” along with files “file.txt” “file2.jpg” “file3.html” will list 4 files: list.php file.txt file2.jpg file3.html

  3. Benji

    @Newb:

    Replace:
    if ($file != “.” && $file != “..”)

    with:
    if ($file != “.” && $file != “..” && $file != “list.php”)

    That should work.

  4. robb

    What about including the date and file size too?

  5. George

    You can use filesize() function to get the file size. To get the last modification date use filemtime().

    Here’s an example:

    List of files

    $file, ‘filemtime’ => filemtime($file), ‘filesize’ => filesize($file));
    }
    }

    closedir($handle);
    }
    ?>

    File nameFile SizeFile Last Modification Time

  6. George

    I don’t know why but the code doesn’t display at all. Anyway, here’s the code: http://pastebin.com/f24b4f242

  7. robb

    OK, the below works great, but where do I add or what do I replace exactly to get the file time and date to show up.

    Note I’ve added to exclude index.php file so it won’t display in list instead of the above which excludes list.php. I also changed ‘.$file.’ to ‘.$file.’ to give a carriage return after each file.

    ‘.$file.’‘;
    }
    }
    closedir($handle);
    }
    ?>
    List of files:

  8. robb

    This forum must be limited on comments size. Here is the full code:
    ‘.$file.’‘;
    }
    }
    closedir($handle);
    }
    ?>
    List of files:

  9. jay

    how would i go about adding the list of files(amount of which will vary) into a dropdown menu?

  10. Martin Crilly

    How can you make the list a list of hyperlinks? so that user can click on the file to open it.

  11. chris

    anyone have some good style sheet / colored layouts that i could try out… i know very little of php, just a few commands for forms and i am trying to learn more

    i have a directory full of web games already in html files, i want to make a php page that displays all the files in the directory but i want it to look kinda like a webpage

  12. Meg

    I’m trying to use this to make an archive of sorts.

    I want it to list a certain amount of recent additions, with the oldest one being taken off from the bottom as new ones come up. I’ll have a link to the complete archives, where its the same concept, recent ones at the time, older ones at the bottoms, but limited to a certain amount per page and once that limit is reached, a new page is generated in addition to.

    I’m a n00b at PHP though. Heh.

  13. Melinda

    Thank you for this information. It helped me out a lot!!

  14. David

    Is there a way to deploy this script and use a drop-down menu to show the list of filenames?

    like say you have a massive directory of MP3s… what im hoping to do is use the code above to gather the filenames, then place them in the drop-down list; so that each value is the file name…

  15. Jim Brouwer

    @ David:
    Sure, it defenitly is possible, quet easy excally.
    Simply replace the last 2 rows with:

    echo ‘

    ‘.$theList.’

    ‘;

    and the part of:
    $thelist .= ‘‘.$file.’‘;
    Would be changed into:
    $thelist .= ”.$file.”;

    Should work out well, goodluck with it!

  16. Jim Brouwer

    Whoops! Just reconized that this form doesn’t allow html…
    So here are the same codes, only this time readable (hopefully) and you just need to replace the x into those things I forgot the name from at the moment.

    xform action=”listen.php” method=”post” name=”mp3List”x
    xselect name=”song”x
    ‘.$theList.’
    x/selectx
    x/formx

    and the part of:
    $thelist .= ‘xa href=”‘.$file.’”x’.$file.’x/ax’;
    Would be changed into:
    $thelist .= ‘xoptionx’.$file.’x/optionx’;

  17. alex

    what about if I want the option to delete some of the file, by clicking delete, how can I doit?

  18. Mohan

    Hi

    Good, The Above Coding is Working

  19. Garrett

    I made a couple changes to this. Now each link is on a different line and you can right click and save it.

    Here it is:
    —————

    To download, right click on the link to the file you want to download and click Save as.
    ‘.$file.’‘;
    }
    }
    closedir($handle);
    }
    ?>
    Here is a list of all the files that have been uploaded:

  20. Stan Swank

    Hello;

    This is great except my files are named

    Ch1-a
    Ch2-b
    Ch3-c

    ETC

    The problem is that I have 10 of them, so the list is
    Ch1-a
    Ch10-a
    Ch10-b
    ch2-a

    How can I fix

  21. Marie ALHOMME

    Hello, I was looking for something just like this, simple and concise, thanks a whole lot ! :)

  22. Lumex

    To display all the files in list format and from another directory, i modified the code somewhat.

    ‘.$file.’‘;
    }
    }
    closedir($handle);
    }
    ?>
    List of files in upload directory:

    Hope this helps some of you

  23. Jeff

    I have directory that gets images added to it each day. I want to list the 10 newest images, showing the images with links.
    From that I can generate an rss feed, but I can’t figure out the PHP for this.

  24. Jen

    This list is working great, but I would like to change the order to display alphabetically instead of oldest-newest. Is this possible?

  25. Sawyer

    Code works great, thanks guys! One question, is it possible to modify the code so that it displays ONLY files starting with a specific sequence of letters, or displays only files in the directory ending with a specific extention? Thanks :) :) :)

  26. Bonie

    Great workd. It worked for me. How do I list the one file per line?

  27. Bogdan Gabara

    The script is great. Works flawlessly
    Is there any way to list the files into a table, on 2 columns?

    file 1 file 2

    file 3 file 4

    and so on…..my idea is simple…but i’m not such a big programmer, so…
    insert all file names into a array, count array, and then output with a “for” or “while”.
    but as I said….I need someone to make it into a script, my head already hurts from this :)
    Thanks in advance

  28. Ege

    ‘.$file.’ ‘;
    $i++ ;
    }
    }
    closedir($handle);
    }

    echo ”
    $y=0
    foreach ($thelist As $listitem) {
    echo ”$listitem,”;
    if ($y=1) {
    $y=0;
    echo ”;
    } else {
    $y=1;
    }
    }
    echo ”
    ?>

  29. Ege

    forget my previous post:

    ‘.$file.’ ‘;
    $i++ ;
    }
    }
    closedir($handle);
    }

    echo ”;
    $y=0;
    foreach ($thelist As $listitem) {
    echo ”,$listitem,”;
    if ($y==1) {
    $y=0;
    echo ”;
    } else {
    $y++;
    }
    }
    echo ”;
    ?>

  30. Ege

    ‘.$file.’ ‘;
    $i++ ;
    }
    }
    closedir($handle);
    }

    echo ”;
    $y=0;
    foreach ($thelist As $listitem) {
    echo ”,$listitem,”;
    if ($y==1) {
    $y=0;
    echo ”;
    } else {
    $y++;
    }
    }
    echo ”;
    ?>

  31. Kristen

    Hi,

    I can’t get this to work. Where exactly do I place it in my code? I’m very new to PHP.

    Thanks!

    Kristen

  32. Didel

    Hi,

    Great piece of scripting, works just great :D

    I liked Lumex’s idea of using it for another directory, however in his post it does not become clear how to do so, so i did some research myself and all the people who would like to do so.

    It is actually very easy. All you have to do is replace this line:

    if ($handle = opendir(’.')) {

    with this:

    if ($handle = opendir(”your/directory/”)) {

    (replace your/directory/ with your own directory, relative to the file in which the whole piece of php code above is used.)

    Greetings from the Netherlands :)

  33. Eugene

    Hi there,
    Is there a way to exclude the file type?

    Like “Page.php” to display only “Page”?

  34. Christal

    Is there a way to add password protection to the files, I have a website that I want people to be able to download files i upload to a directory but only after they have entered a password

  35. Kristen

    I can’t get the exclusion code to work. I’m trying to exclude certain file types from being displayed when I display the directory contents. For instance, the index.php file.

  36. Eugene

    Kirsten:

    Just insert the files like:

  37. Kristen

    Like what?

  38. Eugene

    Just insert the files like:

    ?php
    if ($handle = opendir(’.')) {
    while (false !== ($file = readdir($handle)))
    {
    if ($file != “.” && $file != “..”
    && $file != “NOT-TO-BE-IN-1.php”
    && $file != “NOT-TO-BE-IN-2.html”
    && $file != “NOT-TO-BE-IN-3.jpg”
    && $file != “”
    && $file != “”
    && $file != “”
    && $file != “”
    && $file != “”

    )

    Or have a look at: http://www.php.net/readdir There is almost anything you want there.. Cant find it now but there is code snips that you can choose whitch file types should be displayed.

    I have a problem with the arrangement of the list.

  39. Eugene

    I have a problem with the arrangement of the list.

    Example:

    1.php
    10.php
    100.php
    2.php
    21.php
    3.php

    I would like it to display:

    1.php
    2.php
    3.php
    10.php

    etc.

    The code by Ege does not work.

  40. Ege

    @Eugene;
    because it didn’t show up properly when I copied and pasted it. I will upload it as a text file and post the link here as soon as I get back home (which will be in two days)

  41. Eugene

    Great! Thank you so much Ege!

  42. Eugene

    Thank you very much! that would be great!

  43. Bhrugesh

    well Eugene…
    about the order u wanted to display…
    it can’t be helped if u want to keep the file names just like that…
    because names are read as strings & not numbers…
    if u want 1,2, 3…10, 11, 12…21, 21, 23…
    then u will have to rename files as 01, 02, 03 & so on :)
    hope this helps

  44. Eugene

    Thanks will try that!

  45. Fred

    Ok so basically I want something that will find out what files are in a folder and then php include them into a previously built page.

    Eg. listed/1.php
    listed/2.php
    listed/3.php
    would show up as

    Now I could do this myself but I am expecting 100’s of files that require including. This is what Ive got.

    ‘.”;
    }
    }
    closedir($handle);
    }
    ?>
    List of files:

    I think it has a problem using the

  46. Fred

    grr code doesnt show up properly in here. Never mind.

  47. vick

    sort($file);

    For Accending order of file.


Leave a Comment




Leave your friendly comment here.

If you have a computer help question, click here to leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Our Friends
Getting Started


About How-To Geek
What Is That Process?
svchost.exe
jusched.exe
dwm.exe
ctfmon.exe
wmpnetwk.exe
wmpnscfg.exe
rundll32.exe
wfcrun32.exe
Ipoint.exe
Itype.exe
Wfica32.exe
Mobsync.exe
conhost.exe
Dpupdchk.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. All Rights Reserved.