如何对使用PHP opendir()构建的文件列表进行排序并隐藏扩展名


How to sort a list of files build up with PHP opendir() and hide extensions?

由于我只得到PHP现象的一个非常非常小的部分,我很高兴我通过搜索web创建了以下脚本。正如你所看到的,这显示了我正在构建的网站上的"存档"文件夹中的文件。这是为我们学校的报纸准备的。旧论文将被上传到这个文件夹。一个问题是,目前的秩序非常混乱。我知道PHP可以用sort()排序,但我不确定在这种情况下如何做到这一点。谁能解释一下怎么修好它?

另一个是隐藏扩展。我还没找到,有可能吗?

如果你能至少帮我做第一件事,那你就帮了大忙了。

<?php
    if ($handle = opendir(archive)) {
        $ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
        while (false !== ($file = readdir($handle)))
        {
            if (!in_array($file, $ignore_files))
            {
                $thelist .= '<a href="/archive/'.$file.'">'.$file.'</a>'.'<br>';
            }
        }
        closedir($handle);
    }
?>
<p><?=$thelist?></p>

如果您想从目录中获取文件列表(然后对该列表进行排序),同时剥离扩展名,您可以这样做:

<?php
// $archive variabe assumed to point to '/archive' folder
$ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
// into array $files1 will be every file inside $archive:
$files1 = scandir($archive);
sort($files1);
foreach ($files1 as $file) {
    if (!in_array($file, $ignore_files)) {
        $fileParts = pathinfo($file);
        $thelist .= '<a href="/archive/'.$file.'">'.$fileParts['filename'].'</a>'.'<br>';
    }
}
?>
<p><?=$thelist?></p>