Jump to content

Displaying a list of sorted files + oldest.


nikneven

Recommended Posts

Hi, I was wondering if anyone could help me with the following code:

 

function get_oldest_file($directory) {
    if ($handle = opendir($directory)) {
        while (false !== ($file = readdir($handle))) {
            $files[] = $file;
        }
        foreach ($files as $val) {
            if (is_file($directory.$val)) {
                $file_date[$val] = filemtime($directory.$val);
            }
        }
    }
    closedir($handle);
    asort($file_date, SORT_NUMERIC);
    reset($file_date);
    $oldest = key($file_date);
    return $oldest;
}

echo get_oldest_file("./testDirectory/");

 

As it is right now, it returns the oldest file based on creation, not modification, and it is driving me batty. 

 

I would like it to return the oldest file based on modification,  and i would like it to display the list of files sorted from oldest to newest with their respective time stamps.

 

I've looked at ksort, and timestamps, and I am so very lost, and would greatly appreciate any help. 

 

Thanks again,

~nik

I've found this code on another site and it works.

 

<?php
$i = 0;

if ($handle = opendir('./')) {      //specify path in opendir
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && !is_dir($file)) {
            $files[$i] = strtotime(date ("M d Y H:i:s", filemtime($file)))." $file";
                  $i++;
        }
    }
    closedir($handle);
}

//sorts from increasing(date) use arsort for decreasing(year)

sort($files,SORT_NUMERIC);

$i=0;
foreach ($files as $index => $value)
{
      $modified[$i] = substr($value,11)." was last modified: ".date ("F d Y H:i:s",substr($value,0,20));
      $i++;
}
//prints result as array

foreach ($modified as $val)
{
  echo "$val<br>";
}
?>

Actually, that's returning that they were all created at the same time in 1969....

 

xt was last modified: December 31 1969 19:00:00

was last modified: December 31 1969 19:00:00

was last modified: December 31 1969 19:00:00

was last modified: December 31 1969 19:00:00

t was last modified: December 31 1969 19:00:00

was last modified: December 31 1969 19:00:00

was last modified: December 31 1969 19:00:00

was last modified: December 31 1969 19:00:00

t was last modified: December 31 1969 19:00:00

t was last modified: December 31 1969 19:00:00

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.