views:

138

answers:

4
<div style="width: 49%; height: 300px; float: left; padding-top: 10px; ">

<h2><img src="downloads/general.png"></h2>

    <h1>General Downloads</h1>
    <?php
    $Dirdownloads = opendir('downloads/general');
while (false !== ($file = readdir($Dirdownloads))) {
    if ($file != "." && $file != "..") {
        $files[] = array(
            filemtime('downloads/general/'.$file),
            $file
        );
    }
}
closedir($Dirdownloads);
usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));
foreach ($files as $file) {
    echo '<a href="downloads/general/'.$file[1].'">'.$file[1].'</a><br />';
}
 ?>
 </div>

<div style="width: 49%; height: 300px; float: right; padding-top: 10px; ">

<h2><img src="downloads/conference.jpg"></h2>

    <h1>Conference Information</h1>
 <?php
    $Dirdownloads = opendir('downloads/conference');
while (false !== ($file = readdir($Dirdownloads))) {
    if ($file != "." && $file != "..") {
        $files[] = array(
            filemtime('downloads/conference/'.$file),
            $file
        );
    }
}
closedir($Dirdownloads);
usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));
foreach ($files as $file) {
    echo '<a href="downloads/conference/'.$file[1].'">'.$file[1].'</a><br />';
}
 ?>
 </div>

<div style="width: 49%; height: 300px; float: left; padding-top: 10px; ">

<h2><img src="downloads/newsletter.jpg"></h2>

    <h1>Newsletters</h1>
 <?php
    $Dirdownloads=opendir('downloads/newsletters');
    while (false !== ($file = readdir($Dirdownloads))) {
    if($file != "." && $file != ".."){
        echo '<a href="downloads/newsletters/'.$file.'">'.$file.'</a><br />';
    }
    }
    closedir($Dirdownloads);
 ?>
 </div>

<div style="width: 49%; height: 300px; float: right; padding-top: 10px; ">

<h2><img src="downloads/calendar.png"></h2> 

<h1>Calendar</h1>
 <?php
    $Dirdownloads=opendir('downloads/calendars');
    while (false !== ($file = readdir($Dirdownloads))) {
    if($file != "." && $file != ".."){
        echo '<a href="downloads/calendars/'.$file.'">'.$file.'</a><br />';
    }
    }
    closedir($Dirdownloads);
 ?>
 </div>

Is the code, which displays all the files in a certain folder, and allows you to download them. They are currently arranged by name, and I want to arrange them with newest first... Help?

+1  A: 

Look at: http://us2.php.net/manual/en/function.filemtime.php

Basically compose a 2D array like this:

Array:

[0]File [0]Time

[1]File [1]Time

[2]File [2]Time

...

And so on, the time returned using filemtime() is in UNIX format. So then you can easily create a loop which sorts the list by 'Time'

Sbm007
A: 

If your list of files is small enough that you can actually read them into an array, what I would do is read them into said array, and then use usort with a custom comparison function that filemtimes them to figure which file is newer.

The arguments to usort are the array itself, and a callback that specifies a function which itself receives two elements of the array as argument, and returns negative if the first argument is less than the second, 0 if they are equal, or positive if the first is greater.

notJim
yeah, there is like 5 files in the list ... however, i have 4 lists on each page. can you post the actual code to sort them? this php stuff confuses me :P
steven
stat() produces extra data which can lead to slower (although negligible) performance. I would use filemtime() unless steven intends to display extra file details which stat() provides.
Sbm007
A: 

Hi,

You'll have to :

  • first, get a list of files, with, for each file, its last modification date (this can be fetched using filemtime)
  • then, sort that list, with a user function that sorts depending on the "date" field you've got in you array (to sort using a user-defined function, see usort)
  • and, finally, display the list

For instance, to first get the list of files :

$files = array();

$dir = dirname(__FILE__);
$Dirdownloads=opendir($dir);
while (false !== ($file = readdir($Dirdownloads))) {
    if($file != "." && $file != ".."){
        $files[] = array(
            'name' => $file,
            'date' => filemtime($dir . '/' . $file),
        );
    }
}
closedir($Dirdownloads);

Note that, for each file, I have an array containing "name" and "date".

And, then, to sort them :

usort($files, 'my_sort_function');

And, the sort function that compares files based on the 'date' field :

function my_sort_function($a, $b) {
    if ($a['date'] == $b['date']) {
        return 0;
    }
    return ($a['date'] > $b['date']) ? -1 : 1;
}

With this, the $file array should contain what you want :-)
Depending on the order you want, you'll use either > or < in the sorting function.

Up to you to iterate over that list, to display the links !

Pascal MARTIN
+1  A: 

Collect the files in an array, sort it by creation date and print them after that:

$dir = 'downloads/general';
$Dirdownloads = opendir($dir);
while (false !== ($file = readdir($Dirdownloads))) {
    if ($file != "." && $file != "..") {
        $files[] = array(
            filectime($dir.'/'.$file),
            $file
        );
    }
}
closedir($Dirdownloads);

// sort files by creation time
usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));

// print the files
foreach ($files as $file) {
    echo '<a href="'.$dir.'/'.$file[1].'">'.$file[1].'</a><br />';
}


Edit    As you want to use that algorithms multiple times, a function might be helpful:

function getFilesByCreationDate($dir) {
    $dir = rtrim($dir, '/');
    $handler = opendir($dir);
    while (false !== ($file = readdir($handler))) {
        if ($file != "." && $file != "..") {
            $files[] = array(
                filectime($dir.'/'.$file),
                $file
            );
        }
    }
    closedir($handler);
    usort($files, create_function('$a, $b', 'return $a[0] - $b[0];'));
    foreach ($files as &$file) {
        $file = $file[1];
    }
    return $files;
}

Then you use that function like this:

$dir = 'downloads/general';
$files = getFilesByCreationDate($dir);
foreach ($files as $file) {
    echo '<a href="'.$dir.'/'.$file.'">'.$file.'</a><br />';
}
Gumbo
uhm, now it's not working for the other ones i have on the page (i have 4 categories on the page, each displaying 4 folders.)i've attached the code for the whole page..... please edit/add whatever i did wrong. the page is now displayed in the original question. thansk!
steven
Just wrap that code in a function and you can re-use it.
Gumbo
sorry to sound like a newb, but how would i do that exactly?
steven