Page 1 of 1

File ordering by date modified

Posted: Sat Oct 25, 2008 11:26 am
by semas
Hello everyone, i need some help with file ordering, i need to order files by date modified.
Here is my code:

Code: Select all

 
<?php
$handle = opendir('foto/');
    while ($file = readdir($handle)) {
        $files[] = $file;
    }
    closedir($dir_handle);
    
    sort($files);
    foreach ($files as $k => $file) {
        if($file != "." && $file != ".." && $file != "index.php" && $file != "style.css" && $file != "index2.php" && $file != "0.jpg"){
            $id++;
            if($id_c == $id){
                $s_file = $file;
                $id_cc++;
            }
        }
    }
?>
If someone will help me i'll be realy thankfull.

Re: File ordering by date modified

Posted: Sat Oct 25, 2008 12:45 pm
by Hannes2k
Hi,
just use usort():

There are two ways:
Create a new array (like $files) but don't just save the filename into it. Create for each entry in $files a new array with 2 elements, first element is the filetime (filectime()), second the filename ($file).

Now you can sort the array with usort():

Code: Select all

 
$files = array();
 
//open dir etc.
while(...) {
  $files[] = array(filectime($file), $file);
 
 
//Compares the filectime of to two files
function fileCompare($a, $b) {
    return $a[0] - $b[0];
}
 
usort($files, "fileCompare");
 
//print $files
 

The other would be to call filectime() in the fileCompare function:

Code: Select all

 
$files = array();
 
//Your old while loop
while(...) {
  $files[] = $file;
 
 
//Compares the filectime of to two files
function fileCompare($a, $b) {
    return filectime($a) - filectime($b);
}
 
usort($files, "fileCompare");
 
//print $files
 

Re: File ordering by date modified

Posted: Sat Oct 25, 2008 1:24 pm
by semas
Ok, now im confused. Site made for funny pictures (upload allowed for all with admin confirmation). I tried to aply your exaple, but it ordered everything from about center of all photos.

P.S. If this will not work ill make it with MySQL database.

Re: File ordering by date modified

Posted: Sat Oct 25, 2008 3:18 pm
by Hannes2k
Hi,
just try this:

Code: Select all

<?php
$handle = opendir('foto/');
    while ($file = readdir($handle)) {
        $files[] = $file;
    }
    closedir($dir_handle);
 
//Compares the filectime of two files
function fileCompare($a, $b) {
    return filectime($a) - filectime($b);
}
 
usort($files, "fileCompare");
 
    foreach ($files as $k => $file) {
        if($file != "." && $file != ".." && $file != "index.php" && $file != "style.css" && $file != "index2.php" && $file != "0.jpg"){
            $id++;
            if($id_c == $id){
                $s_file = $file;
                $id_cc++;
            }
        }
    }
?>
In the first example there had been a small mistake, the index of $a and $b in the compare function have to be 0 not 1.
But this code should worke fine (but I didn't tested it)

Re: File ordering by date modified

Posted: Sun Oct 26, 2008 4:19 am
by semas
Ok, thank you for help, but i made it MySql based.

Re: File ordering by date modified

Posted: Mon Oct 27, 2008 12:46 pm
by semas
One more thing. I'm making site chat. Need help with Links that people will post, they use " Link name ".
I used str_replace for another tags, but i think i need preg_match or preg_replace now...

Re: File ordering by date modified

Posted: Mon Oct 27, 2008 1:53 pm
by Hannes2k
Hi,
just use Google and search for: PHP + bb codes.

You use preg_replace for this purpose.