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
https://forums.phpfreaks.com/topic/9521-list-the-contents-of-a-directory/
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.