[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

PHP: Load list of files, sorted by modification time

How can I get a list of files, sorted by modification time?
$dir = dir($path);
    while (false !== ($entry = $dir->read())) {
	echo $entry;
    }
    $dir->close();

Open in new window

0
hankknight
Asked:
hankknight
3 Solutions
 
Greg AlexanderLead DeveloperCommented:
0
 
Ray PaseurCommented:
This looks for PDF files only, but you can probably remove that restriction if you want other kinds of files.
<?php // RAY_temp_filemtime.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');

// THE PATH TO OUR CURRENT WORKING DIRECTORY
$dir = getcwd();

// APPEND A SLASH IF NEEDED
if ($dir[strlen($dir)-1] != DIRECTORY_SEPARATOR) $dir .= DIRECTORY_SEPARATOR;

// AGGREGATE THE DIRECTORY INFORMATION INTO AN ARRAY OF ARRAYS
if ($dh = opendir($dir))
{
    $dir_datas = array();
    while ($file_name = readdir($dh))
    {
        // WE ONLY WANT TO CONSIDER FILES NAMED LIKE *.PDF
        $ext = end(explode('.', $file_name));
        $ext = strtoupper($ext);
        if ($ext == 'PDF')
        {
            $my_name = $dir . $file_name;
            $my_data = array
            ( 'name' => $file_name
            , 'size' => filesize($my_name)
            , 'time' => date('c', filemtime($my_name))
            )
            ;
            $dir_datas[] = $my_data;
        }
    }
}

// IF NO PDFS
if (empty($dir_datas)) die();

// CALL THE FUNCTION TO SORT THE ARRAY BY THE filemtime()
usort($dir_datas, 'timesort');

// GET THE URL PATH
$poz = strrpos($_SERVER["PHP_SELF"], DIRECTORY_SEPARATOR);
$urp = substr($_SERVER["PHP_SELF"], 0, $poz) . DIRECTORY_SEPARATOR;

// CREATE LINKS TO EACH OF THE FILES
$out = NULL;
foreach ($dir_datas as $pdf)
{
    $out .= $pdf["time"];
    $out .= ' ';
    $out .= '<a target="pdf" href="'
    . $urp
    . $pdf["name"]
    . '">'
    . $pdf["name"]
    . '</a>'
    . ' '
    . showfilesize($pdf["size"])
    . '<br/>'
    . PHP_EOL
    ;
}

// CREATE AND DISPLAY THE VIEW OF THE DIRECTORY
$htm
= '<h1>'
. "PUBLICATIONS: "
. date('F j, Y')
. '</h1>'
. $out
;
echo $htm;
die();




// A USER SORT FUNCTION TO ORDER BY DATETIME DESC
function timesort($a, $b, $key='time')
{
    if ($a[$key] == $b[$key]) return 0;
    return ($a[$key] > $b[$key]) ? -1 : 1;
}

// FUNCTION TO PRODUCE AN EASY-TO-READ DESCRIPTION OF THE SIZE OF A FILE
function showFileSize ($xb)
{
    $pb = 1024*1024*1024*1024*1024;
    $tb = 1024*1024*1024*1024;
    $gb = 1024*1024*1024;
    $mb = 1024*1024;
    $kb = 1024;
    if     ($xb >= $pb) { $text = number_format(($xb / $pb),3) . " PB"; }
    elseif ($xb >= $tb) { $text = number_format(($xb / $tb),2) . " TB"; }
    elseif ($xb >= $gb) { $text = number_format(($xb / $gb),1) . " GB"; }
    elseif ($xb >= $mb) { $text = number_format(($xb / $mb),1) . " MB"; }
    elseif ($xb >= $kb) { $text = number_format(($xb / $kb),0) . " KB"; }
    elseif ($xb >= 0)   { $text = number_format( $xb       ,0) . " bytes"; }
    else                { $text = "0 bytes"; }
    return $text;
}

Open in new window

0
 
Beverley PortlockCommented:
This looks very like a problem I addressed here http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26304778.html#33128995 which was to sort by name and creation date, but if you change the comparison function then it should work OK


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now