I got help many moons ago with a snippet of PHP that reads the contents of a directory where the file names are yyyymmdd.pdf and returns a list as an array which gets output as an HTML list. Works great, but I need to modify it for another purpose, and limit the array to the most recent five “dates”.
Have searched and came up with “limit”, but all the references are to SQL queries and that doesn’t apply here.
Here’s my include that gets my list; need to modify this to limit to the most recent five in the directory:
[code]
-
<?
$the_array = Array();
$handle = opendir('images/stories/bulletins/2011/.');
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.html") {
$the_array[] = $file;
}
}
closedir($handle);
sort ($the_array);
reset ($the_array);
// leave the beginning of the script like you had before - get an array of filenames
foreach($the_array as $val) {
// this is the part you should change:
$filesplit = explode('.', $val);
$timestamp = strtotime($filesplit[0]);
$list = date('F j, Y',$timestamp);
echo "
- $list "; } ?>
Thanks much!