jaxdevil Posted September 25, 2008 Share Posted September 25, 2008 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); } ?> Quote Link to comment Share on other sites More sharing options...
jaxdevil Posted September 25, 2008 Author Share Posted September 25, 2008 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> Quote Link to comment Share on other sites More sharing options...
efficacious Posted September 25, 2008 Share Posted September 25, 2008 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); } ?> Quote Link to comment Share on other sites More sharing options...
efficacious Posted September 25, 2008 Share Posted September 25, 2008 I just use $fc variable and incremented it each time a file was outputed Quote Link to comment Share on other sites More sharing options...
efficacious Posted September 25, 2008 Share Posted September 25, 2008 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); } ?> Quote Link to comment Share on other sites More sharing options...
jaxdevil Posted September 25, 2008 Author Share Posted September 25, 2008 That works perfect! Thanks man! SK Quote Link to comment Share on other sites More sharing options...
efficacious Posted September 25, 2008 Share Posted September 25, 2008 np glad to help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.