RSS Feeds for FTP Servers

RSS Feeds for FTP Servers
by Mark Woodman |

The generateRssFeed() Function

This has started to feel like a PHP tutorial, hasn't it? Good news: the hard part is over, and now all that remains is to write the function that actually generates the RSS feed.

This function is the one that we'll call directly from another PHP script, passing in all the parameters needed to make it work:

  • $host: The FTP server hostname. Example: "ftp.foo.com".
  • $user: The FTP username. Example: "anonymous".
  • $pass: The FTP user password. Example: "guest".
  • $path: The starting directory on the server. Example: "/pub/crawl"
  • $itemCount: The number of items to return in the RSS feed.

The first thing we'll do is call our exploreFtpServer() function to get the list of files and their timestamps from the FTP server. Once the list is returned, we can see whether the list is shorter than the $itemCount passed in, and use the smaller number of the two:

function generateRssFeed($host, $user, $pass, $path, $itemCount)

{   

   // Get array of file/time arrays

   $fileList = exploreFtpServer($host, $user, $pass, $path);



   // Use user's count or # of files found, whichever is smaller

   if(count($fileList)<$itemCount) $itemCount=count($fileList);

We have a few variables to declare before continuing. First is a $linkPrefix to hold the hostname prefixed with the FTP protocol. Next is a $channelPubDate which will hold the publication date of the RSS feed. We will also create an $items array to hold the RSS items we create.

   // Create link prefix for feed

   $linkPrefix = 'ftp://' . $host;



   // Declare date for channel/pubDate

   $channelPubDate = null;



   // Array for item strings

   $items = array();

We're now ready to grab the most recent files and create RSS items for each one. For this tutorial, we're going to keep it nice and simple: Each item will have a title, link, and date. Feel free, however, to spice up your feed with more information. (One fun idea would be to display a file icon which matches the file's extension.)

As you'll recall, the $fileList returned from exploreFtpServer() is sorted by timestamp, newest file first. As we loop through the array we'll use the timestamp to create the publication date of the RSS item. The first (newest) timestamp will also be used to create the publication date for the RSS feed as a whole.



   // Create array of RSS items from most recent files

   foreach ($fileList as $filePath => $time)

   {

      // Create item/pubDate according to RFC822

      $itemPubDate = date("r", $time);



      // Also use first item/pubDate as channel/pubDate

      if($channelPubDate==null) $channelPubDate = $itemPubDate;

Next we'll create the file's URI, starting with "ftp://". This $fileUri variable will be used to populate both the RSS item title and the link. We should replace any spaces in the filenames with the encoded value of "%20" to ensure the URI is well-formed.

Now that we have all the information we need, it is time to create the XML for each RSS item. When that is done, we'll add it to our $items array for use later on. We will also be sure to end this loop if we have reached the $itemCount threshold.

      // Create URI to ftp file

      $fileUri = ereg_replace(" ", "%20", $linkPrefix . $filePath);



      // Create item

      $item = '<item>' 

      .'<title>' . $fileUri . '</title>'

      .'<link>'. $fileUri . '</link>'

      .'<pubDate>' . $itemPubDate . '</pubDate>'

      .'</item>';



      // Add to item array

      array_push($items, $item);



      // If max items for feed reached, stop

      if(count($items)==$itemCount) break;

   }

Finally we get to create the RSS feed itself. Building XML in PHP using strings is easy, but rarely pretty to look at. Note that we're using join() to add in all of our RSS items, with a line break after each. (The line breaks aren't necessary, but they make the feed easier to read when you're troubleshooting.)

   // Build the RSS feed

   $rss = '<rss version="2.0">'

   . '<channel>'

   . '<title>FTP Monitor: ' . $host . '</title>'

   . '<link>' . $linkPrefix . '</link>'

   . '<description>The ' . $itemCount.' latest changes on '

   . $host . $path . ' (out of ' . count($fileList) 

   . ' files)</description>'

   . '<pubDate>' . $channelPubDate . '</pubDate>' . "\n"

   . join("\n", $items) . "\n"

   . '</channel>'

   . '</rss>';

The feed is ready to go. All that remains is to set the HTTP header to indicate that we are returning an XML document, then output the feed:

   // Set header for XML mime type

   header("Content-type: text/xml; charset=UTF-8");

   

   // Display RSS feed

   echo($rss);

}

And that's the last of the real work. If you haven't done so yet, be sure to download the complete source code of ftp_monitor.php to see it all in one place.

Prev  [1] [2] [3] [4] Next

Close    To Top
  • Prev Article-XML:
  • Next Article-XML: None
  • Now: Tutorial for Web and Software Design > XML > Metadata > XML Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction