Newest 10 Files in a Directory

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I have some directories which include anywhere from a few dozen files, to a few hundred files. There is one directory that if I slack off on maintaining or there's an unexpected spike in usage for an extended period of time, could potentially include a few thousand files.

I plan on generating RSS feeds using the content and file names of the newest 10 files in each directory according to their last-modified dates.

Keeping in mind that this is a Linux server, and assuming that I can satisfy any and all dependencies, what are some efficient ways I can generate a list of the 10 newest file names using the last-modified time stamp ?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Flanders
  • Beginner
  • Beginner
  • User avatar
  • Posts: 48
  • Loc: Reno, Nevada US

Post 3+ Months Ago

Couldn't you read them into an array and sort by timestamp?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Yes I could, but with what and how is the question.

I want options. :)
  • Flanders
  • Beginner
  • Beginner
  • User avatar
  • Posts: 48
  • Loc: Reno, Nevada US

Post 3+ Months Ago

I will work something up when I get home and post.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

You could have the KEY being the timestamp and the VALUE being the URL or ID to/of the page/file/w.e... just throwing out the idea here...
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I've got a PHP script setup for the time being that catches 404 errors in the RSS directory and dynamicly generates an XML file.

Here's how I'm gathering the filenames in that. (minus security/error checkings)

I can't help but wonder if there's a better way to gather the file names though.
Maybe there's some sort of bash command that would utilize a filesystem index or something.
Sooner or later I will generate the RSS feeds using a cron job every 24 hours.

Code: [ Select ]
if(is_dir($rsspath))
{
    if($dh = opendir($rsspath))
    {
        $files = array();
        while(($file = readdir($dh)) !== false)
        {
            if(substr($file, -4, 4) == '.xml')
            {
                $files[$file] = filemtime("$rsspath/$file");
            }
        }
        closedir($dh);
       
        arsort($files, SORT_NUMERIC);
        foreach(array_slice($files, 0, 10, true) as $wp => $timestamp)
        {
            // Work with entry
        }
    }
}
  1. if(is_dir($rsspath))
  2. {
  3.     if($dh = opendir($rsspath))
  4.     {
  5.         $files = array();
  6.         while(($file = readdir($dh)) !== false)
  7.         {
  8.             if(substr($file, -4, 4) == '.xml')
  9.             {
  10.                 $files[$file] = filemtime("$rsspath/$file");
  11.             }
  12.         }
  13.         closedir($dh);
  14.        
  15.         arsort($files, SORT_NUMERIC);
  16.         foreach(array_slice($files, 0, 10, true) as $wp => $timestamp)
  17.         {
  18.             // Work with entry
  19.         }
  20.     }
  21. }
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

I like using the following piece of code to iterate through a directory and find all of the files/directories in the specified directory.
PHP Code: [ Select ]
<?php
// Iterating through the ./templates directory looking for directories in there
foreach(new DirectoryIterator('./templates/') as $file)
{
   // Making sure that we got a directory that is not the current page
   if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])))
   {
      // Making sure that the current file the pointer is on is actually a directory
      if($file->isDir())
      {
         // Creating the array
         $result[] = $file->getFilename();
      }
   }
}
 
?>
  1. <?php
  2. // Iterating through the ./templates directory looking for directories in there
  3. foreach(new DirectoryIterator('./templates/') as $file)
  4. {
  5.    // Making sure that we got a directory that is not the current page
  6.    if ((!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])))
  7.    {
  8.       // Making sure that the current file the pointer is on is actually a directory
  9.       if($file->isDir())
  10.       {
  11.          // Creating the array
  12.          $result[] = $file->getFilename();
  13.       }
  14.    }
  15. }
  16.  
  17. ?>

You put the URL to the directory (That would be searched through) relative to the directory where the script resides in... other than that, I can't really explain what it's doing other then the comments I put in the code a while back
  • N2S
  • Beginner
  • Beginner
  • User avatar
  • Posts: 48

Post 3+ Months Ago

this script gives you the 5 newest files in the directory..

http://www.3scriptz.com/software/ajax-file-browser.html

I never figured out how to change the limit..
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

That appears to use the same basic method I'm using with fopen/etc.
I was going to look into changing the limit for you, but I didn't see anything about the 5 latest files in the source anywhere.
  • conanite
  • Novice
  • Novice
  • User avatar
  • Posts: 16
  • Loc: paris, france

Post 3+ Months Ago

On linux,

Code: [ Select ]
ls -ct | head -10


gets you the 10 most recently modified files in the current directory ... I don't know php but I guess there's a way to invoke shell commands?
Moderator Remark: added code tags
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

exec()? or system()?
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

conanite wrote:
On linux,

ls -ct | head -10

gets you the 10 most recently modified files in the current directory ... I don't know php but I guess there's a way to invoke shell commands?


This looks like another workable option. :)

Yes there is a way to invoke a shell by using for example, the functions bogey mentioned. This project doesn't have to go utilize PHP though. It could be Perl, Python, a shell script, etc.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

I guess we all assumed you wanted the solution in PHP ...
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Quote:
ls -altF | head -10

Should give you sufficient information, lol
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I played around with bash for a few and came up with this for a program start.

Code: [ Select ]
#!/bin/bash
 
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
echo "<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
echo "  <channel>"
 
for xml in $(ls -ct "/path/to/files" | head -10)
do
    echo "      <item>http://domain.com/`basename ${xml%".xml"}`.html</item>"
done
 
echo "  </channel>"
echo "</rss>"
 
exit 0
  1. #!/bin/bash
  2.  
  3. echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
  4. echo "<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
  5. echo "  <channel>"
  6.  
  7. for xml in $(ls -ct "/path/to/files" | head -10)
  8. do
  9.     echo "      <item>http://domain.com/`basename ${xml%".xml"}`.html</item>"
  10. done
  11.  
  12. echo "  </channel>"
  13. echo "</rss>"
  14.  
  15. exit 0


Which gives me this from a directory where I created in order files named "one.xml" through "five.xml" followed by ".htaccess".
NOTE: I had the -10 argument set to -3 when I executed this so I didn't have to create 11+ test files. ;)

Code: [ Select ]
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <item>http://domain.com/five.html</item>
        <item>http://domain.com/four.html</item>
        <item>http://domain.com/three.html</item>
        ...
    </channel>
</rss>
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  3.     <channel>
  4.         <item>http://domain.com/five.html</item>
  5.         <item>http://domain.com/four.html</item>
  6.         <item>http://domain.com/three.html</item>
  7.         ...
  8.     </channel>
  9. </rss>


I didn't get around to working in the important elements of an RSS feed, but this is yet another way to start. :)
  • N2S
  • Beginner
  • Beginner
  • User avatar
  • Posts: 48

Post 3+ Months Ago

I found this pretty neat code on the web the other day.. It lists all the files in a directory pretty nice..

maybe you can figure out how to take this script apart and limit it..

Code: [ Select ]
<?php
if ($handle = opendir('.')) {
$ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
while (false !== ($file = readdir($handle)))
     {
         if (!in_array($file, $ignore_files))
    {
            $thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
         }
     }
closedir($handle);
}
?>
<p style="font-weight: bold; font-size: 10pt">List of files:</p>
<p style="font-weight: bold; font-size: 10pt; color: #560D1C"><?=$thelist?></p>
  1. <?php
  2. if ($handle = opendir('.')) {
  3. $ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
  4. while (false !== ($file = readdir($handle)))
  5.      {
  6.          if (!in_array($file, $ignore_files))
  7.     {
  8.             $thelist .= '<a href="'.$file.'">'.$file.'</a>'.'<br>';
  9.          }
  10.      }
  11. closedir($handle);
  12. }
  13. ?>
  14. <p style="font-weight: bold; font-size: 10pt">List of files:</p>
  15. <p style="font-weight: bold; font-size: 10pt; color: #560D1C"><?=$thelist?></p>

Post Information

  • Total Posts in this topic: 16 posts
  • Users browsing this forum: No registered users and 123 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.