簡體   English   中英

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

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

您可以使用此功能檢查文件修改時間。

讀取文件,然后將其排序。

有關更多信息

在PHP中按創建/修改日期對文件排序

這將是獲取目錄中所有文件的最后訪問信息的方法

    $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