简体   繁体   English

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

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

As I only get one very very tiny part of the PHP phenomenon I'm very happy that I managed to create the following script by searching the web. 由于我只得到了PHP现象的很小一部分,因此我很高兴能够通过搜索网络来创建以下脚本。 As you can see, this shows the files present in the folder 'archive' on the website I'm building. 如您所见,这显示了我正在构建的网站上“ archive”文件夹中的文件。 It's for the newspaper of my school. 是给我学校的报纸的。 The old papers will be uploaded to this folder. 旧论文将被上传到该文件夹​​。 A problem is that the order is very messy at the moment. 问题在于此刻的订单非常混乱。 I know PHP can sort things with sort(), but I'm not sure how to do that in this case. 我知道PHP可以使用sort()对事物进行排序,但是在这种情况下我不确定如何做到这一点。 Could anyone explain how to get it fixed? 谁能解释一下如何解决它?

Another thing is hiding the extensions. 另一件事是隐藏扩展。 I couldn't find it yet, is there a possibility for that? 我还找不到它,那有可能吗?

You'd be a great help if you could help me at least with the first thing :) 如果您至少可以在第一件事上帮助我,那么您将是一个很大的帮助:)

<?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>

If you want to get the list of files from a directory, (and then sort that list), also stripping off the extension, you would do this: 如果要从目录中获取文件列表(然后对该列表进行排序),同时还要剥离扩展名,则可以执行以下操作:

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM