Jump to content


List the contents of a directory

  • Please log in to reply
1 reply to this topic

#1 jrcarr

  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationBeautiful Arkansas Ozarks

Posted 11 May 2006 - 03:08 AM

I may be trying to make this more complicated than it is, but I'm not mentally coming up with exactly what it is I need here. I will try and explain this the best I can.

I have a directory with a number of files, all being .php. Each week, there is one new file added to the directory. I want to be able to read in all the filenames of the files in this directory, then then output the list into 3 even colums of file names, with the necessary <a></a> so that a visitor can click on it to view the contents of the file.

I know this can be easily done on a static page, but there are currently 170 files and 1 new one every week. I am trying to avoid having to update the page every week to add the new file.

So can anyone give me the basic code to:

1. Read the filenames of all files in a directory into an Array[]
2. start page
3. Calculate 3 equal number of files to list in three colums
4. finish page

I hope this makes sense, but if not feel free to ask for clarification.

Jack Carr

#2 Ferenc

  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 11 May 2006 - 04:47 AM

This is a pretty simple directory reader

$path = "./"; // path to the directory to read ( ./ reads the dir this file is in)
if ($handle = opendir($path)) {
   while (false !== ($file = readdir($handle))) {
    if ($file != "." && $file != "..") {
            $item[] = $file;

$total_items = count($item);
$max_items = ceil($total_items / 3); // items per <td>
$start = 0;
$end = $max_items
//generate the table
<table width="100%" border="1" cellspacing="0" cellpadding="2">
    for($i=0; $i<3; $i++){
        if($i != 0){
            $start = $start + $max_items;
            $end   = $end + $max_items;
        echo "<td>";
        for($x = $start; $x < $end; $x++){
            // display the item
            echo '<a href = "'.$path.$item[$x] .'">';
            echo $item[$x]."</a><br>";
    echo "</td>";

Everything you want to know is here.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users