简体   繁体   English

PHP包含最新日期的文件

[英]php include file with newest date

I'm trying to make a sort of blog with php and I want to include the 5 newest files in an other directory by modification time. 我正在尝试使用php创建博客,并且希望在修改时间之前将5个最新文件包含在其他目录中。

I can inculde them all one by one each time I have made a new file, but it would be nice if I can just include the newest 5. 每次制作一个新文件时,我都可以一一地将它们全部感染,但是如果我只包含最新的5个,那就太好了。

Because i don't want to put the modification date in the file name I really don't know how to do it. 因为我不想将修改日期放在文件名中,所以我真的不知道该怎么做。

Is there a way to do that? 有没有办法做到这一点?

$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;
     }
}

Then include the whole files in the array 然后将整个文件包含在数组中

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

You can get list files with array for eg. 您可以获取带有数组的列表文件,例如。

$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/zh/function.filemtime.php

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

You can check the file modification time with this functions. 您可以使用此功能检查文件修改时间。

Read the file sort it with that time. 读取文件,然后将其排序。

More info on 有关更多信息

Sorting files by creation/modification date in PHP 在PHP中按创建/修改日期对文件排序

This would be a way to get last access info of all files in a directory 这将是获取目录中所有文件的最后访问信息的方法

    $p = "path";


    $a = scandir($p);


    foreach($a as $f){

    $last_access = fileatime($p."/".$f);

    }

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

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