I'm trying to create a very simple php blog, have got as far as including files in a directory and only including the newest file in index.php.
I would like for only a certain section of the newest file to display, can this be done?
Current code used on index.php is below.
<?php
$files = glob('blog/*.php');
sort($files);
$newest = array_pop($files);
include $newest;
?>
http://php.net/manual/en/function.include.php
When you include a file, it is just like taking that files contents and adding to your script at that location. You need to have logic in your included file to determine what to display. The include
statement along will not do what you want.
If you're looking for the newest file by modification time, you need to access mtime
field of data, which will be provided by stat() function:
$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);
However, you can achieve that via shell-call and command like:
find . -type f -printf '%T@ %p
' | sort -n | tail -1 | cut -f2- -d" "