php - readdir sort by filename where file name is the name of the months -
<?php $current_dir = "/members/downloads/board-meetings/2014/"; // full path directory $dir = opendir($current_dir); // open directory echo (""); while ($file = readdir($dir)) // while loop { $parts = explode(".", $file); // pull apart name , dissect period if (is_array($parts) && count($parts) > 1) { $extension = end($parts); // set see last file extension if ($extension == "pdf" or $extension == "pdf") // pdf docs extention echo "<li class=\"pdf\"><strong><a href=\"/members/downloads/board-meetings /$file\" class=\"underline\" target=\"_blank\">$file</a></strong></li>"; // if so, echo out! } } echo "<br>"; closedir($dir); // close directory ?>
i hoping ask expert. code works great except site needs list filenames month. named like: january.pdf, february.pdf etc... , need listed in reverse monthly order. december.pdf, november.pdf etc... , get: october.pdf november.pdf april.pdf - way off base. ideas extremely appreciated.
during first iteration, calculate month ordinal number , create array whereby month kept in keys , file name in values.
$current_dir = "/members/downloads/board-meetings/2014/"; // full path directory $months = array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'); $dir = opendir($current_dir); // open directory $files = array(); while ($file = readdir($dir)) { $extension = strtolower(pathinfo($file, pathinfo_extension)); $month = array_search(substr($file, 0, 3), $months); if ($extension == 'pdf') { $files[$month] = $file; } }
then, add sorting step.
krsort($files);
finally, iterate sorted array:
foreach ($files $file) { echo "<li class=\"pdf\"><strong><a href=\"/members/downloads/board-meetings /$file\" class=\"underline\" target=\"_blank\">$file</a></strong></li>"; // if so, echo out! } echo "<br>"; closedir($dir); // close directory
Comments
Post a Comment