Listing files in order of date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Opix
Forum Newbie
Posts: 4
Joined: Sun Jun 30, 2002 5:52 am
Location: Behind you!
Contact:

Listing files in order of date

Post 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
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post 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.
Opix
Forum Newbie
Posts: 4
Joined: Sun Jun 30, 2002 5:52 am
Location: Behind you!
Contact:

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Opix
Forum Newbie
Posts: 4
Joined: Sun Jun 30, 2002 5:52 am
Location: Behind you!
Contact:

Post 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>";
Post Reply