我想創建一個非常簡單的PHP博客,儘可能包括文件在目錄中,只包括最新的文件在index.php。
我想只顯示最新文件的某個部分,可以這樣做嗎?
index.php上使用的當前代碼如下。
<?php
$files = glob('blog/*.php');
sort($files);
$newest = array_pop($files);
include $newest;
?>
我想創建一個非常簡單的PHP博客,儘可能包括文件在目錄中,只包括最新的文件在index.php。
我想只顯示最新文件的某個部分,可以這樣做嗎?
index.php上使用的當前代碼如下。
<?php
$files = glob('blog/*.php');
sort($files);
$newest = array_pop($files);
include $newest;
?>
http://php.net/manual/en/function.include.php
當你有一個文件,它就像服用該文件的內容,並在該位置添加到您的腳本。您需要在包含文件中有邏輯來確定要顯示的內容。 include
聲明將不會做你想要的。
如果你正在尋找通過修改時間最新的文件,你需要訪問數據的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);
但是,您可以通過殼 - 實現這一目標呼叫和類似命令:
find . -type f -printf '%[email protected] %p\n' | sort -n | tail -1 | cut -f2- -d" "
d你有什麼錯誤? $ newest變量是否包含任何內容?你是否試圖迴應它?ň – DeiForm