Page 1 of 1
Listing files in order of date
Posted: Sun Jun 30, 2002 5:52 am
by Opix
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
Posted: Sun Jun 30, 2002 6:27 am
by will
this is actually a modification of the code someone else posted on these boards regarding the same type of question
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]);
}
this leaves you with an array of the files in the directory sorted by modification time.
Posted: Sun Jun 30, 2002 7:02 am
by Opix
Small problem. When i tried using the following code:
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]);
}
?>
it just prints:
Array
Array
Array
Array
any ideas why?
Posted: Sun Jun 30, 2002 9:01 am
by twigletmac
It's because $filetoprint is an array. In,
Code: Select all
$fileArrayї] = array($file,$staї'mtime']);
You are putting an array within an array so you get a multidimensional array.
Try,
Code: Select all
foreach ($fileArray as $filetoprint) {
list($printfile, $stuff) = $filetoprint;
echo $printfile;
}
Mac
Posted: Sun Jun 30, 2002 2:18 pm
by Opix
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>";