重新排序从目录生成的列表

时间:2013-08-09 20:13:57

标签: php html

我有这个代码,它将从网站上的文件夹(平面目录)中提取信息,并生成一个文件列表,供使用php下载。它查看文件名的前两个字符,并根据此“前缀”为每个部分创建一个Month标题。这使它看起来不是“平坦的”。

效果很好,但我需要将最新的文件放在顶部而不是底部。我可以通过重新排列数组键轻松更改标题的顺序。

我很感激有关如何做到这一点的建议。这是代码:

<?php
$prevgroupprefix = '';

if ($handle = opendir('bulletin')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $groupprefix = substr($file,0,2);
            $period = strrpos($file, ".");
            $filepath = "bulletin/" . $file;
            $file = substr($file,0,$period);
            if ($groupprefix <> $prevgroupprefix){
            $title = "$groupprefix Weekly Bulletins";
            $month = array(
                    "01" => "January",
                    "02" => "February",
                    "03" => "March",
                    "04" => "April",
                    "05" => "May",
                    "06" => "June",
                    "07" => "July",
                    "08" => "August",
                    "09" => "September",
                    "10" => "October",
                    "11" => "November",
                    "12" => "December"
                );

                Switch($groupprefix)
                {
                    Case "01":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[01]</div>";
                        break;

                    Case "02":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[02]</div>";
                        break;

                    Case "03":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[03]</div>";
                        break;

                    Case "04":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[04]</div>";
                        break;

                    Case "05":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[05]</div>";
                        break;

                    Case "06":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[06]</div>";
                        break;

                    Case "07":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[07]</div>";
                        break;

                    Case "08":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[08]</div>";
                        break;

                    Case "09":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[09]</div>";
                        break;

                    Case "10":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[10]</div>";
                        break;

                    Case "11":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[11]</div>";
                        break;

                    Case "12":
                        $title = "<div id='bulletinmonth'>$groupprefix - $month[12]</div>";
                        break;

                    Case ".D":
                        $title = "";
                        break;
                }
                echo "<tr><td higth='50%'>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>";
                echo "<tr><td>&nbsp;</td><td> $title </td><td>&nbsp;</td></tr>";
            }
            echo "<div id='bulletinbody'><ul><li><a href='$filepath'target='_blank'> $file </a></li></ul></div>";
            $prevgroupprefix = $groupprefix;
        }
    }
    closedir($handle);
}
?> 

1 个答案:

答案 0 :(得分:0)

也许您应该使用scandir(返回所有文件的数组),然后使用array_reverse