RSS Feeds for FTP Servers

RSS Feeds for FTP Servers

by Mark Woodman
March 22, 2006

The applications for RSS have extended far beyond a way to distribute news items. RSS is now used for everything from tracking packages to car dealer inventories. These reflect one of the great aspects of RSS: you can use it to tell you when something happens that you care about, rather than having to go check for yourself. In that spirit, this article will show you how to write a PHP script that will monitor an FTP server for you, notifying you of the newest files added or changed.

PHP, FTP, and Thee

With all the emphasis on web-related functionality, the FTP commands in PHP are often overlooked. The good news is that these functions are included with standard PHP 4, so no external libraries are required.

It is important to make sure your PHP install has the FTP functions enabled, however. To do this, use phpinfo() in a simple file to see what has been enabled:

<?php  

   phpinfo();  

?>

When you view the above script in your web browser, you'll see the classic PHP Info page with nearly all the configuration information you'll ever need. Scroll down to the FTP section to see if "FTP support" has been "enabled." It should look something like this:

PHP Info with FTP enabled
Figure 1. PHP Info showing FTP is enabled

(If the FTP functions are not enabled, you'll need to make arrangements to either get it enabled or host this tutorial's script on a different server.)

It is a lot to absorb, but the PHP Manual on FTP functions is a useful reference. You may want to keep it close at hand while going through this tutorial. (And if you have insomnia, you could always try reading RFC 959, the specification for FTP itself.)

Know the Code

For this tutorial, we will create a PHP script called ftp_monitor.php. We will go through the script piece by piece, but you might also want to download the complete source code for reference.

The exploreFtpServer() Function

Let's start with the heart of the FTP functionality, encapsulated in the exploreFtpServer() function. This will take parameters for the FTP hostname, username, password, and initial path. The purpose of this function is to explore the FTP server, recurse through any directories, and then return an associative array of filenames and corresponding file timestamps.

After declaring the function signature, use PHP's ftp_connect() to attempt a connection to the FTP server. If the connection is created, we will keep the connection ID in the variable $cid to be used with all of PHP's FTP-related functions.

function exploreFtpServer($host, $user, $pass, $path)

{

   // Connect

   $cid = ftp_connect($host) or die("Couldn't connect to server");

For the sake of simplicity, the above code will summarily halt the script execution if a FTP connection cannot be established. After you have used this script for a while, you may want to add in some more robust error handling.

Once we have a connection, we will attempt to authenticate with the server by using the PHP function ftp_login(). Like most of PHP's FTP functions, the first argument is the connection ID ($cid). This particular one also takes a username and password.

If the login is successful, we'll use ftp_pasv() to tell the FTP server we are going to use passive mode. This means all connections will be initiated by the script. In so doing, you will be able to run this script behind a firewall.

Now that the connection is all set up, we can recurse through the FTP server directories, starting at the specified path. We'll use a scanDirectory() function to accomplish this, to be written after this one.

Finally, whether authentication worked or not, we'll need to close the connection to the server using ftp_close(). Again, this example will halt the script with die() if authentication fails, but you may choose to handle the failure in a different way.

   // Login

   if (ftp_login($cid, $user, $pass))

   {

      // Passive mode

      ftp_pasv($cid, true);



      // Recurse directory structure

      $fileList = scanDirectory($cid, $path);



      // Disconnect

      ftp_close($cid);

   }

   else

   {

      // Disconnect

      ftp_close($cid);



      die("Couldn't authenticate.");

   }

At this point we have a populated $fileList variable, which is an associative array. The keys are the file names, and the values are the timestamps of the files. This array will be most useful if sorted by timestamp--newest first--so sort it with arsort() and return it.

   // Sort by timestamp, newest (largest number) first

   arsort($fileList);



   // Return the result

   return $fileList;

}

[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