TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Happy Holidays Everyone! now to my question : ) limiting results from read dir (http://www.talkphp.com/absolute-beginners/3813-happy-holidays-everyone-now-my-question-limiting-results-read-dir.html)

Lou_Gato 12-28-2008 05:56 PM

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);
?>


Village Idiot 12-29-2008 02:21 AM

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.


All times are GMT. The time now is 05:42 AM.

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