[英]How to sort a list of files build up with PHP opendir() and hide extensions?
由於我只得到了PHP現象的很小一部分,因此我很高興能夠通過搜索網絡來創建以下腳本。 如您所見,這顯示了我正在構建的網站上“ archive”文件夾中的文件。 是給我學校的報紙的。 舊論文將被上傳到該文件夾。 問題在於此刻的訂單非常混亂。 我知道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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.