<?php
$current_dir = "/members/downloads/board-meetings/2014/"; // Full path to directory
$dir = opendir($current_dir); // Open directory
echo ("");
while ($file = readdir($dir)) // while loop
{
$parts = explode(".", $file); // Pull apart the name and dissect by period
if (is_array($parts) && count($parts) > 1) {
$extension = end($parts); // Set to see last file extension
if ($extension == "pdf" OR $extension == "PDF") // PDF DOCS by extention
echo "<li class='"pdf'"><strong><a href='"/members/downloads/board-meetings /$file'" class='"underline'" target='"_blank'">$file</a></strong></li>"; // If so, echo it out!
}
}
echo "<br>";
closedir($dir); // Close the directory
?>
我希望向专家寻求一些帮助。此代码效果很好,除了此站点需要按月列出文件名。它们被命名为:一月.pdf日、二月.pdf等等...... 并且它们需要按每月反向顺序列出。所以12月.pdf,然后是11月.pdf等等... 我得到:十月.pdf十一月.pdf四月.pdf - 远离基地。 任何想法将不胜感激。
在第一次迭代期间,计算月份序号并创建一个数组,其中月份保存在键中,文件名保存在值中。
$current_dir = "/members/downloads/board-meetings/2014/"; // Full path to 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;
}
}
然后,添加排序步骤。
krsort($files);
最后,迭代排序数组:
foreach ($files as $file) {
echo "<li class='"pdf'"><strong><a href='"/members/downloads/board-meetings /$file'" class='"underline'" target='"_blank'">$file</a></strong></li>"; // If so, echo it out!
}
echo "<br>";
closedir($dir); // Close the directory