Subscribe to How-To Geek

Recommended: Click Here to Run a Free Scan for Common PC Errors   [Sponsored Link]

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.

The Geek is the founder of How-To Geek and a geek enthusiast. When he's not coming up with great how-to articles, he's probably writing at his personal blog. This article was written on 09/25/06 and tagged with: Programming, PHP

Comments (14)

  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…


Leave a Comment




Leave your friendly comment here. If you have a computer help question, leave it on the forums instead.

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

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