Jump to content

List the contents of a directory


jrcarr

Recommended Posts

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
Link to comment
Share on other sites

This is a pretty simple directory reader

[code] <?php
$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 != "..") {
        if(!is_dir($file)){
            $item[] = $file;
            }
       }
   }
   closedir($handle);
}

$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">
  <tr>
    <?php
    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>";
    }
    ?>
  </tr>
</table>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.