0

I've got this code:

<?php 
  foreach (glob("*files/*.html") as $filename) { 
  echo "<iframe src=$filename></iframe>"; 
} 
?>

The structure is like this:

|-index.php
|-files 
   |- are.html
   |- hello.html
   |- who.html
   |- you.html

The problem is: it isn't sorted on date. I tried it with filemtime (http://php.net/manual/en/function.filemtime.php) with this code:

<?php 
  foreach (glob("*files/*.html") as $filename) { 
  echo "<iframe src=$filename></iframe>"; 
}
if (file_exists($filename)) {
echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
?> 

but it only echoes 1 of the 4 html's I got in that directory: you.html How can I let it echo all 4 html's on last modified date?

  • see this post:http://stackoverflow.com/questions/124958/glob-sort-by-date – Suchit kumar Oct 31 '15 at 10:28
  • http://imgur.com/nnLZHi6 it doesnt work :/ . It should be in this order: hello.html who.html are.html you.html –  Oct 31 '15 at 12:45

1 Answers1

0

This works :)

$myFiles = glob("*files/*.html");
usort($myFiles, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));

foreach ($myFiles as $filename) { 
echo "<iframe src=$filename></iframe>"; 
}