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)) &#123; 
$sta = stat($dir.$file); $fileArray&#1111;] = array($file,$sta&#1111;'mtime']);
&#125; 
closedir($d); usort($fileArray,'timeSort'); foreach ($fileArray as $filetoprint) &#123; 
echo "$filetoprint
"; 
&#125; 

function timeSort($a, $b) &#123; 
return -strcmp($a&#1111;1], $b&#1111;1]); 
&#125;
?>
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&#1111;] = array($file,$sta&#1111;'mtime']);
You are putting an array within an array so you get a multidimensional array.

Try,

Code: Select all

foreach ($fileArray as $filetoprint) &#123; 
    list($printfile, $stuff) = $filetoprint;
    echo $printfile; 
&#125;
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)) &#123;

$sta = stat($topicsdir.$viewtopicfile);
$topicarray&#1111;] = array($viewtopicfile,$sta&#1111;'mtime']);
&#125;
closedir($dir);
usort($topicarray,'timeSort');


function timeSort($a, $b)
&#123;
return -strcmp($a&#1111;1], $b&#1111;1]);
&#125;

foreach ($topicarray as $topicarraytouse) &#123;
list($topicfiletouse,$stuff) = $topicarraytouse;
if (($topicfiletouse != ".") && ($topicfiletouse != "..")) &#123;
$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>";
&#125;&#125;
$topictable .= "</table></td></tr></table>";