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); } ?> Link to comment https://forums.phpfreaks.com/topic/125806-solved-limit-files-displayed-from-listing-files-in-a-directory/ 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> Link to comment https://forums.phpfreaks.com/topic/125806-solved-limit-files-displayed-from-listing-files-in-a-directory/#findComment-650511 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); } ?> Link to comment https://forums.phpfreaks.com/topic/125806-solved-limit-files-displayed-from-listing-files-in-a-directory/#findComment-650516 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 Link to comment https://forums.phpfreaks.com/topic/125806-solved-limit-files-displayed-from-listing-files-in-a-directory/#findComment-650520 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); } ?> Link to comment https://forums.phpfreaks.com/topic/125806-solved-limit-files-displayed-from-listing-files-in-a-directory/#findComment-650525 Share on other sites More sharing options...
jaxdevil Posted September 25, 2008 Author Share Posted September 25, 2008 That works perfect! Thanks man! SK Link to comment https://forums.phpfreaks.com/topic/125806-solved-limit-files-displayed-from-listing-files-in-a-directory/#findComment-650527 Share on other sites More sharing options...
efficacious Posted September 25, 2008 Share Posted September 25, 2008 np glad to help Link to comment https://forums.phpfreaks.com/topic/125806-solved-limit-files-displayed-from-listing-files-in-a-directory/#findComment-650546 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.