TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-18-2009, 12:40 AM   #1 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default 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 
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Old 02-18-2009, 01:17 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Smile

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);
}
Attached Files
File Type: zip TalkPHP_Fetch_Latest_Articles.zip (1.8 KB, 15 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
buildakicker (02-19-2009)
Old 02-19-2009, 07:55 PM   #3 (permalink)
The Acquainted
 
buildakicker's Avatar
 
Join Date: Jan 2008
Posts: 119
Thanks: 21
buildakicker is on a distinguished road
Default

That's great. I'll implement it here and tell you how it works.
__________________
SkiLeases.com
buildakicker is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[How To] Allow / Disallow Directory Browsing dynaweb Tips & Tricks 6 04-14-2009 12:57 PM
Break if no file found in directory Peuplarchie Advanced PHP Programming 2 11-17-2008 02:31 AM
Aptana Jaxer and file uploads xenon Advanced PHP Programming 2 06-06-2008 10:22 AM
Writing to XML file buildakicker General 8 02-06-2008 08:17 PM
Determine if file was uploaded? Andrew Absolute Beginners 3 12-08-2007 11:45 PM


All times are GMT. The time now is 06:33 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design