Sign In/My Account | View Cart  
advertisement


Listen Print Discuss

RSS Feeds for FTP Servers
by Mark Woodman | Pages: 1, 2, 3, 4

Put It to Work

After placing ftp_monitor.php on your PHP-enabled web server, you can reference it from any other PHP script. Here is an example of how that might look:

<?php
  
   // Import the FTP Monitor
   require_once('ftp_monitor.php');
    
   // Connection params to monitor FreeBSD snapshots
   $host = "ftp.freebsd.org";
   $user = "anonymous";
   $pass = "guest@anon.com";
   $path = "/pub/FreeBSD/snapshots";
   
   // Generate RSS 2.0 feed showing newest FreeBSD snapshots
   generateRssFeed($host, $user, $pass, $path, 10);
?>

Here is a sample output file from the above connection parameters: freebsd.xml. When viewed in SharpReader, the items look like this:

PHP Info with FTP Enabled
Figure 2. FTP Monitor items for ftp.freebsd.org

Many RSS aggregators will automatically follow an item's link if it does not have a description element. SharpReader is one of these aggregators, and it also supports the ftp:// protocol. Thus, clicking on one of the items from our FTP monitor will start to download it automatically. This usually works just fine if the FTP server allows anonymous connections. If you had to provide a real username and password in ftp_monitor.php, however, your ability to "click and download" will depend on whether your RSS reader can prompt you for FTP credentials.

Enhance Your Performance

There are a few caveats to keep in mind when using this script. First, many FTP servers aren't exactly speedy, so the performance of this script will be bound by the response times of the FTP commands themselves. Simply put, the more directories it has to recurse, the longer it will take. Try to limit the scope of what you need to monitor.

Second, this script is not intended to be hit by a lot of concurrent users. The speed issue is one factor, but the other is FTP connections. For every concurrent hit to this script, a connection is made to the FTP server. It won't take much for the available connections to max out. So, if you want to provide an RSS feed to a lot of users, you should hide this script behind a cache which calls it on a periodic basis. Let the real load be handled by your cache, not the ftp_monitor script.

If you keep these constraints in mind, you can provide a nice service to your users which provides the information they need without frustrating response times.

Give It Back

If you find this script useful, or if you come up with a cool modification to it, I'd love to hear from you. Post a comment and share what you have learned with the xml.com community.


Comment on this articleShare your experience in our forums.
(* You must be a
member of XML.com to use this feature.)
Comment on this Article


Titles Only Titles Only Newest First
  • doesn`t work for me
    2006-12-15 16:20:23 Teresa3455 [Reply]

    i`ve been looking for such a feature for a long time, but for some reason I can`t get it to work, no matter which ftp I use win or linux based, ftp functions are installed for php. Either I get a blank page whith only rss feed info showing or I get timeout and foreach function errors. Maybe someone can tweak this script a bit so it would work on any ftp server

  • weak reject
    2006-03-31 20:14:21 olpa3 [Reply]

    I'm a bit depressed by the article. It's too weak.


    First of all, I'm not sure that phpinfo() qualifies for a section to discuss. A pair of phrases is enough.


    <quote>We should replace any spaces in the filenames with the encoded value of "%20" to ensure the URI is well-formed.</quote>


    That's not enough. File names in UNIX can contain eany symbols except '\0', including <, & and >. More, non-ASCII letters do also exist. They should be %-escaped in UTF8 encoding.


    And small recommendation about code typing. The following:



    $rss = '<rss version="2.0">'
    . '<channel>'
    . '<title>FTP Monitor: ' . $host . '</title>'
    . '<link>' . $linkPrefix . '</link>'
    ...


    has a more convinient alternative:



    $rss = <<<EOT
    <rss version="2.0">
    <channel>'
    <title>FTP Monitor: $host</title>
    <link>$linkPrefix</link>
    ...
    EOT;


    --
    olpa @ http://xmlhack.ru/


  • setting the headers fails
    2006-03-27 15:18:29 mariuss [Reply]

    I tried your script on my server and setting the content type header on line 83 fails, no idea why. I had to silence this error (by adding @ in front of header) to get it going.


    Also, as you mention, for performance reasons it would be much better to implement this as a cron run script that generates a static xml file.