简体   繁体   English

显示以最新文件开头的目录中的所有文件

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

I am trying to show all my files in a directory beginning with newest file added in directory.我试图在以目录中添加的最新文件开头的目录中显示我的所有文件。 Right now I can only include all the files with the following code.现在我只能包含具有以下代码的所有文件。

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

I also managed to include only the most recent file, but I cant seem to figure out how to do it all together.我还设法只包含最新的文件,但我似乎无法弄清楚如何一起做。 I would be happy for any help.我很乐意提供任何帮助。 Thanks谢谢

First fill an array with information - filename and change date首先用信息填充数组 - 文件名和更改日期

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

Then sort it by the change date然后按更改日期排序

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

And finally, do something with the sorted list最后,对排序列表做一些事情

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

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

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