TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   How do I find the most recently uploaded file in a directory? (http://www.talkphp.com/general/3984-how-do-i-find-most-recently-uploaded-file-directory.html)

buildakicker 02-18-2009 12:40 AM

How do I find the most recently uploaded file in a directory?
 
Hello all,

I would like to use php to find the most recently updated / uploaded files inside of my root directory. I would like to echo the link to the files. I kind of have an idea...

PHP Code:

Use php directory traversal
would I find the $saved_date 
date('F jS, Y'filemtime(__FILE__)); ?
How do I get the $saved_date page titleJS
Display link to 5 latest updated pages 


Wildhoney 02-18-2009 01:17 AM

1 Attachment(s)
You did well to come up with a concept :-) For that good work by yourself, I have spent a little time writing you up a little function to make it happen. If you have any questions regarding the code, please ask. This way you will learn. I've tried to comment it as best as I could, however. I hope it helps you!

php Code:
function TalkPHP_Fetch_Latest_Articles($iLimit)
{
    /* Get all the files in the current directory ending in .html. */
    $aFiles = glob('*.html');
    $aPages = array();
   
    foreach ($aFiles as $szFile)
    {
        /* Construct two arrays to hold the items to sort below. */
        $aTimes[] = filemtime($szFile);
        $aPages[] = $szFile;
    }
   
    /* Sort the files by time modified descending, this will give us the order. */
    array_multisort($aTimes, SORT_DESC, $aPages, SORT_REGULAR, $aFiles);
    $aPages = array();
   
    /* Loop through the files in the correct order as changed above. */
    foreach ($aFiles as $iIndex => $szFile)
    {
        /* Break from the loop if we have the amount we desire. */
        if ($iIndex >= $iLimit)
        {
            break;
        }
       
        /* Get the file contents and use regular expressions to extract the title. */
        $szContent = file_get_contents($szFile);
       
        /* We are looking for the title between <title>This</title>. */
        preg_match('~<title.*?>(?P<title>.+?)</title>~is', $szContent, $aMatches);
       
        /* Place the location to the file and its title into an array. */
        $aPages[] = (object) array
        (
            'file' => $szFile,
            'title' => $aMatches['title']
        );
    }
   
    /* Return the pages limited to the amount we specified when we called the function. */
    return $aPages;
}

$aLatestArticles = TalkPHP_Fetch_Latest_Articles(5);

/* Loop through the pages and display them to the end user! */
foreach ($aLatestArticles as $pLatestArticle)
{
    printf(" - %s (%s)\n", $pLatestArticle->title, $pLatestArticle->file);
}

buildakicker 02-19-2009 07:55 PM

That's great. I'll implement it here and tell you how it works.


All times are GMT. The time now is 01:27 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0