[英]PHP Include $newest Issue
我正在嘗試創建一個非常簡單的php博客,目前為止在目錄中包括文件,而在index.php中僅包括最新文件。
我只想顯示最新文件的某個部分,可以這樣做嗎?
在index.php上使用的當前代碼如下。
<?php
$files = glob('blog/*.php');
sort($files);
$newest = array_pop($files);
include $newest;
?>
如果要通過修改時間查找最新文件,則需要訪問數據的mtime
字段,該字段將由stat()函數提供:
$rgFiles = glob('blog/*.php');
usort($rgFiles, function($sFileOne, $sFileTwo)
{
$rgStatOne = stat($sFileOne);
$rgStatTwo = stat($sFileTwo);
return $rgStatOne['mtime']<$rgStatTwo['mtime']?-1:$rgStatOne['mtime']!=$rgStatTwo['mtime'];
});
$sFile = array_pop($rgFiles);
但是,您可以通過shell調用和命令來實現,例如:
find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "
http://php.net/manual/zh/function.include.php
包含文件時,就像獲取文件內容並在該位置添加腳本一樣。 您需要在包含的文件中包含邏輯以確定要顯示的內容。 include
語句將不會執行您想要的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.