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 12-28-2008, 05:56 PM   #1 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 14
Thanks: 0
Lou_Gato is on a distinguished road
Default Happy Holidays Everyone! now to my question : ) limiting results from read dir

ok I learned how to read dir, and list the results.. now my issue is can i limit the results from the directory to only the 3 most recent files in that directory? lets say that the files are images, and I would only like to see the latest three images in that directory. no more. how would i achieve this?


Code:
<?
  $desired_extension = 'jpg'; //extension we're looking for
  $dirname = "../thumbs/";
  $dir = opendir($dirname);

  while(false != ($file = readdir($dir)))
  {
    if(($file != ".") and ($file != ".."))
    {
      $fileChunks = explode(".", $file);
      if($fileChunks[1] == $desired_extension) //interested in second chunk only
      {      
        echo '<a href="http://online-tech.com/clubtoni/thumbs/'.$file.'"> '.$file.'</a></br>';
      }
    }
  }
  closedir($dir);
?>
Lou_Gato is offline  
Reply With Quote
Old 12-29-2008, 02:21 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Use the filetime function to get the unix timestamp of each file and the sort function to sort them.

ps. To get the extension for all files (including things like file.inc.ext), use sizeof(array)-1 to get the last chunk.
__________________

Village Idiot 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


All times are GMT. The time now is 02:28 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