1

Using the below action I gather a list of files and display them. I need to be able to sort the files by last modified date. Is there a simple way in PHP or using ZEND to order the items by last modified? If you know of a easier way to get the files and order them using ZEND; please let me know.

public function imagesAction()
{
    $this->_helper->layout->disableLayout();

    $results = array();

    $handler = opendir(APPLICATION_PATH . '/../public/images/blog');

    while ($file = readdir($handler)) {
        if ($file != "." && $file != ".." && $file != '.svn') {
            $results[] = $file;
        }
    }

    closedir($handler);

    $this->view->data = $results;
}

I have tried doing

$this->view->data = ksort($results);
$this->view->data = asort($results);

But those just remove the entire list of files from the view and they stop showing up.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
ILikeTurtles
  • 1,012
  • 4
  • 16
  • 47
  • Does your result array contain data before you do ksort and asort? What about trying to sort before you do $this -> view -> data i.e Use $sort = ksort($results); and then $this->view->data = $sort; – ajtrichards Nov 26 '12 at 20:50
  • @WebChemist I am trying to find a way to translate the above code into using ZEND if possible. – ILikeTurtles Nov 26 '12 at 20:51
  • @socialrel8 - Yes; the code currently functions as is. I am able to get a result and populate my fields with the data but I need it to be sorted by last modified. I will try to create a intermediary variable and send that to the view. – ILikeTurtles Nov 26 '12 at 20:52

3 Answers3

2
public function imagesAction()
{
    $this->_helper->layout->disableLayout();

    $results = array();

    $handler = opendir(APPLICATION_PATH . '/../public/images/blog');

    while ($file = readdir($handler)) {
        if ($file != "." && $file != ".." && $file != '.svn') {
            $results[] = array('file' => $file, 'time' => filemtime($file));
        }
    }

    closedir($handler);

    uasort($results, function($file1, $file2) {
        if ( $file1['time'] == $file2['time'] )
           return 0;
        return $file1['time'] < $file2['time'] ? -1 : 1;
    });

    $this->view->data = $results;
}
  • Now I am getting the below; shouldn't it include a time? Array ( [16] => Array ( [file] => SS-2012.04.30-18.48.54_art3.png [time] => ) [15] => Array ( [file] => Headliner Valve.jpg [time] => ) } – ILikeTurtles Nov 26 '12 at 21:16
  • 1
    I think you must provide the absolute path to file. – Alexandru Chelariu Nov 26 '12 at 21:23
  • You are correct. I added the absolute path and changed the way my view handled the return. It works perfectly. Thank you so much! – ILikeTurtles Nov 26 '12 at 21:29
  • PHP7.3+ `uasort($results,function($file1,$file2){ return $file1['time']<=>$file2['time']; });` – a55 Feb 20 '21 at 11:53
0

Use the last modified date as the key:

while ($file = readdir($handler)) {
    if ($file != "." && $file != ".." && $file != '.svn') {
        $s = stat($file);
        if (!is_array($results[$s['mtime']]) {
             $results[$s['mtime']] = array();
        }
        $results[$s['mtime']][] = $file;
    }
}

Then use ksort to sort by key:

ksort($results);

Every item of $results now holds an array of filenames that are modified at the same time.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Watch out, `ksort()` like basically all array sorting functions returns true or false and acts directly on the array. – Adrian World Nov 27 '12 at 13:45
0

You can use a DirectoryIterator ( http://php.net/manual/en/class.directoryiterator.php ) to easy iterate over the files in a directory and get the properties of each (including last modified time).

You could simply instantiate the iterator, iterate through it and sort the items into an array of your choosing.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103