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);
}
?> 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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);
  }
?>

Link to comment
Share on other sites

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);
  }
?>

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.