簡體   English   中英

顯示以最新文件開頭的目錄中的所有文件

[英]Show all files from directory beginning with newest file

我試圖在以目錄中添加的最新文件開頭的目錄中顯示我的所有文件。 現在我只能包含具有以下代碼的所有文件。

<?php
foreach (glob("posts/*.php") as $filename)
{
include $filename;
}
?>

我還設法只包含最新的文件,但我似乎無法弄清楚如何一起做。 我很樂意提供任何幫助。 謝謝

首先用信息填充數組 - 文件名和更改日期

$files = array();
foreach( glob( "posts/*.php" ) as $filename ) {
   $files[] = array(
     "name" => $filename,
     "change_date" => filectime( $filename );
   );
}

然后按更改日期排序

usort( $files, function($a, $b) {
   return $a->change_date - $b->change_date;
} );

最后,對排序列表做一些事情

foreach ( $files as $file ) {
  echo $file;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM