jonoc33 Posted January 7, 2008 Share Posted January 7, 2008 Hi guys, I have a script which lists a bunch of files in a folder: <?php $folder = FILE_FOLDER; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; } if($files == 0){ echo "<br />"; } ?> How exactly would I limit this to only show 5 of the most recent files, and if thats not possible, just 5 files in the folder. Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/ Share on other sites More sharing options...
adam291086 Posted January 7, 2008 Share Posted January 7, 2008 You will need to add a counter into your while loop. <?php $i=0; if($i<5) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; $i++; } } else { closedir($handle); #echo the files foreach ($files as $file) { echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; } if($files == 0){ echo "<br />"; } } ?> not 100% sure if that will work Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-432573 Share on other sites More sharing options...
jonoc33 Posted January 7, 2008 Author Share Posted January 7, 2008 I got no output. Using this code: <?php $folder = FILE_FOLDER; $handle = opendir($folder); $i=0; if($i<5) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; $i++; } } else { closedir($handle); #echo the files foreach ($files as $file) { #160;   echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; } if($files == 0){ echo "<br />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-432575 Share on other sites More sharing options...
adam291086 Posted January 7, 2008 Share Posted January 7, 2008 try this <?php $folder = FILE_FOLDER; $handle = opendir($folder); $i=0; if($i<5) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; $i++; } #echo the files foreach ($files as $file) { echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; } if($files == 0){ echo "<br />"; } } else { closedir($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-432579 Share on other sites More sharing options...
jonoc33 Posted January 7, 2008 Author Share Posted January 7, 2008 Ah, that worked. But it still displays all files in the folder.. Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-432580 Share on other sites More sharing options...
adam291086 Posted January 7, 2008 Share Posted January 7, 2008 Add this to the top of the php and tell me any errors ini_set ("display_errors", "1"); error_reporting(E_ALL); edit.... Also try this <?php $folder = FILE_FOLDER; $handle = opendir($folder); $i=0; if($i<5) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; $i++; } } #echo the files foreach ($files as $file) { echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; } if($files == 0){ echo "<br />"; } else { closedir($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-432582 Share on other sites More sharing options...
jonoc33 Posted January 7, 2008 Author Share Posted January 7, 2008 No errors Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-432583 Share on other sites More sharing options...
adam291086 Posted January 7, 2008 Share Posted January 7, 2008 Try <?php $folder = FILE_FOLDER; $handle = opendir($folder); $i=0; while($i<5) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } $i++; } #echo the files foreach ($files as $file) { echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; } if($files == 0){ echo "<br />"; } else { closedir($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-432585 Share on other sites More sharing options...
jonoc33 Posted January 7, 2008 Author Share Posted January 7, 2008 Strange. The code seems like it would do it, but i'm still receiving over 5 outputs.. Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-433102 Share on other sites More sharing options...
teng84 Posted January 7, 2008 Share Posted January 7, 2008 Try <?php $folder = FILE_FOLDER; $handle = opendir($folder); $i=0; while($i<5) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } $i++; } #echo the files foreach ($files as $file) { echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; } if($files == 0){ echo "<br />"; } else { closedir($handle); } ?> wrong .... i guess <?php $folder = FILE_FOLDER; $handle = opendir($folder); $i=0; while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; $i++; if ($i == 5){break;}//-----------------------see this } #echo the files foreach ($files as $file) { echo "-<a href=$folder".rawurlencode($file).">$file</a><br />"; } if($files == 0){ echo "<br />"; } else { closedir($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-433105 Share on other sites More sharing options...
jonoc33 Posted January 7, 2008 Author Share Posted January 7, 2008 Ok that worked, and for some weird reason it displayed 3, lol. nevermind, fixed. Thankyou all Link to comment https://forums.phpfreaks.com/topic/84853-solved-limiting-an-output/#findComment-433108 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.