Web Hosting Talk







View Full Version : PHP opendir(), sorting by date?


Joshua44
11-15-2002, 03:59 PM
Is there some way to sort the files gotten by the opendir() function?




$newboard = strtolower($board);
$pat = "/1.php$/i";
$dir = opendir("$newboard/postdata/");
while(($filename = readdir($dir)) !== false) {
if ($filename == '.' || $filename == '..') continue;
if (preg_match($pat,$filename)) {


}
}
}


Right now I'm using that, but at first it would just get the files randomly. Now I have it so that it prints them backwards, and only two at a time. :( help?

sasha
11-15-2002, 06:14 PM
you could
if (preg_match($pat,$filename)) {

$files[filemtime($newboard/postdata$filename)] = $filename ;

}

ksort ($files); //sort by time

Novicane
11-16-2002, 03:57 AM
http://www.phpbuilder.com/mail/php-general/2002081/1902.php

<?php
// Note that !== did not exist until 4.0.0-RC2
$dir='/home';
if ($handle = opendir($dir))
{
echo "Directory handle: $handle\n";
$lasttime=0;
$temp=array();
while (false !== ($file = readdir($handle)))
{
echo $file;
if(is_file($file))$temp[$file]=filemtime($file);
}
}
asort($temp,SORT_NUMERIC);
reset($temp);
while(list($k,$v)=each($temp))
echo $k.':'.date('r',$v)."\n";
?>

Joshua44
11-18-2002, 11:40 PM
ok, but now the array only holds to values! When one file comes in, the next leaves..... btw, I'm using the first bit of code up there.
Help pls??? Thanks..