SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    USA
    Posts
    1,407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can you sort files by date using readdir()

    Code is below. I want to sort these in DESC order according the file date. Is there way to do that?

    PHP Code:
    $dir "../mydir/";
    $dh  opendir($dir);
    //    get file names
    while (false !== ($filename readdir($dh))) {
        if(
    $filename !="." AND $filename !=".."){
        echo 
    "<li>".substr($filename0, -4)."</li>";
        }

    I could always add a database entry to the process but I am trying to keep it simple.

    Thanks for any help.

  2. #2
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $dir "../mydir/";
    chdir($dir);
    array_multisort(array_map('filemtime', ($files glob("*.*"))), SORT_DESC$files);
    foreach(
    $files as $filename)
    {
        echo 
    "<li>".substr($filename0, -4)."</li>"


  3. #3
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    USA
    Posts
    1,407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Brilliant. Many thanks!

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're using PHP5 it has dedicated flags to sort files in a directory using the scandir function


Bookmarks

Posting Permissions

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