0

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;
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user1739696
  • 127
  • 11
  • Do you have any error ? Does the variable $newest contain anything ? Have you tried to echo it ?ň – DeiForm Aug 20 '13 at 14:31

2 Answers2

1

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\n' | sort -n | tail -1 | cut -f2- -d" "

combined with exec() or similar (full variant see here)

Community
  • 1
  • 1
Alma Do
  • 37,009
  • 9
  • 76
  • 105
0

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.

Schleis
  • 41,516
  • 7
  • 68
  • 87