0

I am working on a script that will display all files and make links in a giving directory, but i want the most recently uploaded to be at the top of the list. I have searched through this site looking for an answer or a similar question but came up with nothing.

Here is the code I have so far:

<?php
$dir = opendir('files/'); 
while ($read = readdir($dir)) 
{
if ($read!='.' && $read!='..') 
{ 
echo '<ul style="list-style: none;"><li><a href="files/'.$read.'"target="_blank"><FONT COLOR="#0196e3">'.$read.'</a></li></ul>'; 
}
}
closedir($dir); ?>

I would appreciate any help or suggestions.

h4ck3r
  • 207
  • 2
  • 15

2 Answers2

0

Store the files in an array with the last modified time, and then order the array before displaying it. Try the below, untested:

$dir = opendir('files/');
$fileArray = array();    

while ($read = readdir($dir)) 
{
    if ($read!='.' && $read!='..') 
    { 
        // Get the time this directory was last edited and store it in an array
        $fileArray[] = array('fileName' => $read, 'lastModified' => filemtime($dir));
    }
}

closedir($dir);

// Sort the files
$usort($fileArray, 'compareFiles');

foreach($fileArray as $file)
{
    // files sorted now - display them here
}

// Compares the files - see: http://www.php.net/manual/en/function.usort.php
function compareFiles($a, $b)
{
    if ($a[lastModified] == $b[lastModified]) {
        return 0;
    }
    return ($a[lastModified] < $b[lastModified]) ? -1 : 1;
}
dKen
  • 2,927
  • 1
  • 23
  • 33
  • I get an error at this line $fileArray[] = array('fileName' => $read, 'lastModified' => filemtime($dir)); – h4ck3r May 31 '13 at 09:11
  • @Redstone "I get an error" doesn't help. What error? This code is untested, but it should give you an idea of how to solve your problem. I've commented it with links to further reading. You should try to understand how to solve it instead of just copying it and pasting it blindly. Tell me what the error was and I'll have a look. – dKen May 31 '13 at 09:19
  • I'm reading through "filemtime" and "usort" right now I think that might be what I need but every time I use it I get the following error (filemtime() expects parameter 1 to be string). – h4ck3r May 31 '13 at 09:29
  • Ok I got filemtime working but, it is not displaying most recent uploaded it is displaying the first that was modified. Is there any to check the upload time? to put the most recently uploaded at top even if the file was modified before another that is already uploaded. – h4ck3r May 31 '13 at 10:00
  • Great, glad you're most of the way there. Looks like we just need to get the creation time instead of last modified. This depends on the OS - Windows has a create time, but most Unix filesystems don't store the create date. See: http://stackoverflow.com/questions/4401320/php-how-can-i-get-file-create-date. If you're not on Windows, it may not be possible. – dKen May 31 '13 at 10:43
  • Thank you I will begin researching on how to do this on a Linux system. – h4ck3r May 31 '13 at 10:55
0

I use scandir. Description in following.

Vlad Bereschenko
  • 328
  • 3
  • 11