I'm trying to list the files in a certain directory in order of which has been last updated (most recent at the top) then output the results to html. What's the simplest way to do this?
Thanks in advance
-Opix
Listing files in order of date
Moderator: General Moderators
this is actually a modification of the code someone else posted on these boards regarding the same type of question
this leaves you with an array of the files in the directory sorted by modification time.
Code: Select all
$dir = "/whatever/directory";
$fileArray = array();
$d = opendir($dir);
while ($file = readdir($d)) {
$sta = stat($dir.$file);
$fileArrayї] = array($file,$staї'mtime']);
}
}
closedir($d);
usort($fileArray,'timeSort');
function timeSort($a, $b)
{
return -strcmp($aї1], $bї1]);
}Small problem. When i tried using the following code:
it just prints:
Array
Array
Array
Array
any ideas why?
Code: Select all
<?
$dir = "topics";
$fileArray = array();
$d = opendir($dir);
while ($file = readdir($d)) {
$sta = stat($dir.$file); $fileArrayї] = array($file,$staї'mtime']);
}
closedir($d); usort($fileArray,'timeSort'); foreach ($fileArray as $filetoprint) {
echo "$filetoprint
";
}
function timeSort($a, $b) {
return -strcmp($aї1], $bї1]);
}
?>Array
Array
Array
Array
any ideas why?
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
It's because $filetoprint is an array. In,
You are putting an array within an array so you get a multidimensional array.
Try,
Mac
Code: Select all
$fileArrayї] = array($file,$staї'mtime']);Try,
Code: Select all
foreach ($fileArray as $filetoprint) {
list($printfile, $stuff) = $filetoprint;
echo $printfile;
}have you any idea why this doesn't work. the files aren't in the right order:
Code: Select all
$topicsdir = "topics/";
$topicarray = array();
$dir = opendir($topicsdir);
$topictable = "<table width="50%" border="1" bordercolor="333333" align="center">";
while ($viewtopicfile = readdir($dir)) {
$sta = stat($topicsdir.$viewtopicfile);
$topicarrayї] = array($viewtopicfile,$staї'mtime']);
}
closedir($dir);
usort($topicarray,'timeSort');
function timeSort($a, $b)
{
return -strcmp($aї1], $bї1]);
}
foreach ($topicarray as $topicarraytouse) {
list($topicfiletouse,$stuff) = $topicarraytouse;
if (($topicfiletouse != ".") && ($topicfiletouse != "..")) {
$viewtopicfilenotxt = eregi_replace(".txt", "", $topicfiletouse);
$getvarsfortopic = $viewtopicfilenotxt;
require "data/$getvarsfortopic.var";
$query = "topictxt=topics/$viewtopicfilenotxt.txt";
$topictable .= "<tr><td width="50%" bgcolor="333333"><img src="smilies/$icon.gif"> - <a href="viewtopic.php?$query">$viewtopicfilenotxt</a></td><td><div align="center"><b>$poster</b> $date</div></td></tr>";
}}
$topictable .= "</table></td></tr></table>";