PHP 包含具有最新日期的文件


php include file with newest date

我正在尝试用php制作一种博客,我想在修改时间之前将5个最新文件包含在另一个目录中。

每次制作新文件时,我都可以将它们一一灌输,但如果我可以只包含最新的 5 个,那就太好了。

因为我不想把修改日期放在文件名中,我真的不知道该怎么做。

有没有办法做到这一点?

$path = "/path/to/directory"; //the directory path 
$latest5files = array();    //array for latest 5 files to be stored
$d = dir($path);
while ((false !== ($entry = $d->read())) || count($latest5files) < 5) 
{
     $filepath = "{$path}/{$entry}";
     if ((is_file($filepath))) 
     {
         $latest_filename[] = $entry;
     }
}

然后将整个文件包含在数组中

for ($i = 0; $i < count($latest5files); $i++)
{
    include $latest5files[ $i ];
}

您可以获取带有数组的列表文件,例如。

$a[1234567890] = 'file1.php';
arsort($a);
foreach(array_slice($input, 0, 5) as $fTime => $file):

http://php.net/manual/en/function.filemtime.php

http://php.net/manual/en/function.stat.php

您可以使用此功能检查文件修改时间。

阅读文件,用那个时间排序。

更多信息

在 PHP 中按创建/修改日期对文件进行排序

这将是获取目录中所有文件的最后访问信息的一种方式

    $p = "path";

    $a = scandir($p);

    foreach($a as $f){
    $last_access = fileatime($p."/".$f);
    }