我想创建一个非常简单的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