我正在尝试使用 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
您可以使用此功能检查文件修改时间。
读取文件按那个时间排序。
更多信息
这将是一种获取目录中所有文件的最后访问信息的方法
$p = "path";
$a = scandir($p);
foreach($a as $f){
$last_access = fileatime($p."/".$f);
}