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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Displaying newest file

    Hi,

    is anyone aware of any php scripts currently available that would allow me to search a folder containing a number of subfolders and then display the title (less extension) of the newest file in the combined directories (with a link to the file in question)? So far I've only come across ones that allow searching of single, specified directories.

    Essentially, we have a series of folders of church notice sheets as pdfs and I'd like our home page to have a link to the latest one (current structure is year/month/file.pdf)

    Many thanks.
    Last edited by picea73; 12-14-2008 at 05:23 PM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Works great - thanks very much!

    Without sounding cheeky, how would I convert the code to only display the latest entry?

    Thanks again.

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Try
    PHP Code:
    echo $Files[count($Files)-1]; 
    at the end.
    Last edited by abduraooft; 12-15-2008 at 12:11 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    all I get it the word "Array" printed out.

    Code I'm using (prior to the above) is:


    <?php

    function LoadFiles($dir,$filter="")
    {
    $Files = array();
    $It = opendir($dir);
    if (! $It)
    die('Cannot list files for ' . $dir);
    while ($Filename = readdir($It))
    {
    if ($Filename != '.' && $Filename != '..' )
    {
    if(is_dir($dir . $Filename))
    {
    $Files = array_merge($Files, LoadFiles($dir . $Filename.'/'));
    }
    else
    if ($filter=="" || preg_match( $filter, $Filename ) )
    {
    $LastModified = filemtime($dir . $Filename);
    $Files[] = array($dir .$Filename, $LastModified);
    }

    else
    continue;

    }
    }
    return $Files;
    }
    function DateCmp($a, $b)
    {
    return strnatcasecmp($a[1] , $b[1]) ;
    }

    function SortByDate(&$Files)
    {
    usort($Files, 'DateCmp');
    }

    $Files = LoadFiles("documents/Noticesheet/");
    SortByDate($Files);
    reset($Files);
    while (list($k,$v) =each($Files))
    {
    echo "<a href='";
    ?><?=$v[0]?><?
    echo "'>Latest Weekly Noticesheet";
    echo "</a><br>";
    }

    ?>

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    'Tis an array.
    Depends on you're ordering (I'm too tired to figure it out from code reading, yeesh :P)
    You'll either use: $aFilesArray[0] or $aFilesArray[$i] where $i = count($aFilesArray) - 1 (depending on if its asc sorted or desc sorte).

    I swear that I posted a solution for you on this earlier today. Mine was simplier
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Oh.. my bad, I hadn't tested.
    PHP Code:
    echo $Files[count($Files)-1][0]; 
    (count(), not strlen() :-( )
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great - works fantastic! Thanks a lot for the help.


    For anyone else reading, the final code is:


    <?php

    function LoadFiles($dir,$filter="")
    {
    $Files = array();
    $It = opendir($dir);
    if (! $It)
    die('Cannot list files for ' . $dir);
    while ($Filename = readdir($It))
    {
    if ($Filename != '.' && $Filename != '..' )
    {
    if(is_dir($dir . $Filename))
    {
    $Files = array_merge($Files, LoadFiles($dir . $Filename.'/'));
    }
    else
    if ($filter=="" || preg_match( $filter, $Filename ) )
    {
    $LastModified = filemtime($dir . $Filename);
    $Files[] = array($dir .$Filename, $LastModified);
    }

    else
    continue;

    }
    }
    return $Files;
    }
    function DateCmp($a, $b)
    {
    return strnatcasecmp($a[1] , $b[1]) ;
    }

    function SortByDate(&$Files)
    {
    usort($Files, 'DateCmp');
    }

    $Files = LoadFiles("documents/Noticesheet/");
    SortByDate($Files);

    echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href='";

    echo $Files[count($Files)-1][0];
    echo "'>Latest&nbsp;Weekly&nbsp;Noticesheet";
    echo "</a><br>";

    ?>


  •  

    Posting Permissions

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