Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Apr 2008
    Location
    Indiana
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts

    List files in a directory by time of modification.

    I'm using the following script (found the original online and modified it) to list the files in a given directory with links to them, opening the files for display in an iframe. The problem is, I want to list the files newest to oldest, newest being at the top.
    PHP Code:
    <?php

    $folder 
    "files/";
    $handle opendir($folder);

    # Making an array containing the files in the current directory:
    while ($file readdir($handle))
    {
        if ( 
    $file != ".." && $file != "." )
        {
        
    $files[] = $file ;
        }
    }
    closedir($handle);

    #echo the files
    foreach ($files as $file) {
        echo 
    "<a href=$folder$file target='read'>$file</a>"."<br />" ;     
    }
    ?>
    If anyone could shed some light on how (and where) I might do this, it would be greatly appreciated.
    Last edited by fluxnuk3r; 03-27-2009 at 05:20 PM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by fluxnuk3r View Post
    I'm using the following script (found the original online and modified it) to list the files in a given directory with links to them, opening the files for display in an iframe. The problem is, I want to list the files newest to oldest, newest being at the top.
    PHP Code:
    <?php

    $folder 
    "files/";
    $handle opendir($folder);

    # Making an array containing the files in the current directory:
    while ($file readdir($handle))
    {
        if ( 
    $file != ".." && $file != "." )
        {
        
    $files[] = $file ;
        }
    }
    closedir($handle);

    #echo the files
    foreach ($files as $file) {
        echo 
    "<a href=$folder$file target='read'>$file</a>"."<br />" ;     
    }
    ?>
    If anyone could shed some light on how (and where) I might do this, it would be greatly appreciated.
    you can use filemtime to get the timestamp of last change, use it as key in the array and sort the array by keys using ksort.

    http://www.php.net/manual/en/function.filemtime.php
    http://www.php.net/manual/en/function.ksort.php

    best regards

  • #3
    New Coder
    Join Date
    Apr 2008
    Location
    Indiana
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I read both pages you linked. Although I understand what they do, I don't understand how I can not only use them together, but how I can patch my script with them. I have tried using the filemtime function, but I haven't been using PHP long enough to understand where to put it in the script to make it work.

    I am all for reading about the suggestions and trying to come to the answer, but I just don't know where to go with this. The example on the ksort() page does not really explain to me where to put the filemtime function as a key to ksort. Since my array is $files[] = $file, I am guessing that as opposed to the example shown, I won't use an array as a key, but the filemtype function. Which brings me back to the fact that I don't know how to do that.

    Thanks again!

  • #4
    New Coder
    Join Date
    Apr 2008
    Location
    Indiana
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I've experimented a little with the functions. I did get some results, but not really what I wanted. Here is what it looks like now:

    PHP Code:
    <?php
    $folder 
    "files/";
    $handle opendir($folder);

    # Making an array containing the files in the current directory:
    while ($file readdir($handle))
    {   
        if ( 
    $file != ".." && $file != "." )
        {
        
    $files[] = $file ;
        }
    }
    closedir($handle);

    #echo the files
    ksort($files) ;
    $key filemtime();

    foreach (
    $files as $file) {
        echo 
    "<a href=$folder$file target='read'>$file</a>"."<br />" ;
    }
    ?>
    ksort does sort the files, and it seemed at first as though it was sorting them by modification date (which didn't make sense to me, $key is defined, but is not used anywhere). I created 3 files in files/: "jnew", "new", and "znew". I used the first letters to make sure they were not sorted alphabetically. It listed the three files in descending order of modification, which was good. But, then I made the file "snew" and it was not placed at the top as it should have been. The order should be snew, znew, new, jnew. Instead, it became znew, jnew, snew, new. "snew", being the newest file, should be at the top. But it isn't.

    I'm obviously going about this in the wrong way..

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    - filemtime must have as argument the file
    - ksort sort a array using keys and as I said the keys of your array must be the timestamp of the file. Try this:

    PHP Code:
    <?php
    $folder 
    "files/";
    $handle opendir($folder);

    # Making an array containing the files in the current directory:
    while ($file readdir($handle)){   
        if( 
    $file != ".." && $file != "." ){
            
    $key filemtime($file);
            
    $files[$key] = $file ;
        }
    }
    closedir($handle);
    // sort files by mtime:
    ksort($files) ;

    #echo the files
    foreach ($files as $file) {
        echo 
    "<a href=$folder$file target='read'>$file</a>"."<br />" ;
    }
    ?>
    best regards

  • Users who have thanked oesxyl for this post:

    fluxnuk3r (03-27-2009)

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    If you use the filemtime() value as the key, there will certainly be collisions on files that were last modified at the same time.

    Have a look at array_multisort:
    PHP Code:
    // grab the files
    $files glob'./folder/*.*' );

    // re-order the files
    $files array_multisort(
        
    // order by modified time, ascending
        
    array_map'filemtime'$files ),
        
    SORT_ASC,
        
    SORT_NUMERIC,
        
    // order by file name on similar times, ascending
        
    array_map'basename'$files ),
        
    SORT_ASC,
        
    SORT_STRING,
        
    // the array of files we are ordering
        
    $files
    ); 
    This example is unested, but should get you on your way.
    Last edited by kbluhm; 03-27-2009 at 01:42 PM.

  • Users who have thanked kbluhm for this post:

    oesxyl (03-27-2009)

  • #7
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Create an array containing the files and then use uasort() to sort it. Ex:
    PHP Code:
    <?php

    function newest($a$b)
    {
        return (
    filemtime($a) > filemtime($b)) ? -1;
    }

    $dir glob('files/*'); // put all files in an array
    uasort($dir"newest"); // sort the array by calling newest()

    foreach($dir as $file)
    {
        echo 
    '<a href="' $file '" target="read">' basename($file) . '</a><br />';
    }

    ?>

  • Users who have thanked Nicklas for this post:

    fluxnuk3r (03-27-2009)

  • #8
    New Coder
    Join Date
    Apr 2008
    Location
    Indiana
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you all for your help (its working now)...

    oesxyl: the script did list the newest file, but only the newest file. the others somehow dissappeared. I added the new line and it only showed the one file. I thought that perhaps I had missed something so I copied the entire script and it still only returned one file.

    kbluhm: thank you for your script as well. I was slightly confused by it and went on to Nicklas's post. If his hadn't worked I certainly would have tried to figure out how to use yours.

    Nicklas: You did it! I'm not entirely sure how exactly, so I'll be investigating that. It lists all the files in the directory by modification time, newest to oldest. Thank you!

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by fluxnuk3r View Post
    Thank you all for your help (its working now)...

    oesxyl: the script did list the newest file, but only the newest file. the others somehow dissappeared. I added the new line and it only showed the one file. I thought that perhaps I had missed something so I copied the entire script and it still only returned one file.
    this is probably because of keys( see kbluhm post about collisions). I guess the simplest way to fix it is
    to use filenames as keys, because are uniq, and timestamp as values, but this time you must use asort instead of ksort.

    best regards

  • #10
    New Coder
    Join Date
    Apr 2008
    Location
    Indiana
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Never mind. The problem I had here was due to a design "oops".
    Last edited by fluxnuk3r; 03-28-2009 at 01:25 AM.


  •  

    LinkBacks (?)


    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •