Jump to content

[SOLVED] Limit files displayed from listing files in a directory


jaxdevil

Recommended Posts

I am trying to list the last say 5 files in my directory. I have a code snippet to list the files. Problem is also that this code displays two lines above the list of files. A single period on the first line and two periods on the second line. So first, how can I make this limit to just 5 files, and second, why the periods above the results?

 

Thank in advance.

 

<?php
if ($handle = opendir($_SERVER[DOCUMENT_ROOT].'/files/db/')) {
    while (false !== ($file = readdir($handle))) {
        echo "$file<br>";
    }
    closedir($handle);
}
?> 

Here is a better code for listing the files, but I still can't make it limit the results returned.?

 

<?php
if ($handle = opendir($_SERVER[DOCUMENT_ROOT].'/files/db/')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
  {
          	$thelist .= '<a href="'.$file.'">'.$file.'</a>';
          }
       }
  closedir($handle);
  }
?>
<?=$thelist?><br>

add a for loop or something in there..

<?php
if ($handle = opendir($_SERVER[DOCUMENT_ROOT].'/files/db/')) {
   while (false !== ($file = readdir($handle)))
      {$fc=0;
          if ($file != "." && $file != ".." && $fc<6)
          {
          	$thelist .= '<a href="'.$file.'">'.$file.'</a>';
             $fc++;
          }
       }
  closedir($handle);
  }
?>

oops my fault

 

add a for loop or something in there..

<?php
if ($handle = opendir($_SERVER[DOCUMENT_ROOT].'/files/db/')) {
$fc=0;//MOVED OUTSIDE THE  WHILE LOOP SO IT DOESN"T RESET TO ZERO
while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != ".." && $fc<6)
          {
          	$thelist .= '<a href="'.$file.'">'.$file.'</a>';
             $fc++;
          }
       }
  closedir($handle);
  }
?>

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.