Online Book Reader

Home Category

Webbots, Spiders, and Screen Scrapers - Michael Schrenk [59]

By Root 341 0
a routine to send a short email notification when commands fail. Automated email error notification allows the script to run autonomously without requiring that someone verify the operation manually.[43] Listing 13-2 shows the email configuration script.

include("LIB_MAIL.php");

$mail_addr['to'] = "admin@somedomain.com";

$mail_addr['from'] = "admin@somedomain.com";

function report_error_and_quit($error_message, $server_connection)

{

global $mail_addr;

// Send error message

echo "$error_message, $server_connection";

formatted_mail($error_message, $error_message, $mail_addr, "text/plain");

// Attempt to log off the server gracefully if possible

ftp_close($server_connection);

// It is not traditional to end a function this way, but since there is

// nothing to return or do, it is best to exit

exit();

}

Listing 13-2: Email configuration

The next step is to make a connection to the remote FTP server. After making the connection, the script authenticates itself with its username and password, as shown in Listing 13-3.

// Negotiate a socket connection to the remote FTP server

$remote_connection_id = ftp_connect(REMOTE_FTP_SERVER);

// Log in (authenticate) the source server

if(!ftp_login($remote_connection_id, REMOTE_USERNAME, REMOTE_PASSWORD))

report_error_and_quit("Remote ftp_login failed", $remote_connection_id);

Listing 13-3: Connecting and authenticating with the remote server

Once authenticated by the server, the script moves to the target file's directory and downloads the file to the local filesystem. After downloading the file, the script closes the connection to the remote server, as shown in Listing 13-4.

// Move the directory of the source file

if(!ftp_chdir($remote_connection_id, REMOTE_DIRCTORY))

report_error_and_quit("Remote ftp_chdir failed", $remote_connection_id);

// Download the file

if(!ftp_get($remote_connection_id, "temp_file", REMOTE_FILE, FTP_ASCII))

report_error_and_quit("Remote ftp_get failed", $remote_connection_id);

// Close connections to the remote FTP server

ftp_close($remote_connection_id);

Listing 13-4: Downloading the file and closing the connection

The final task, shown in Listing 13-5, uploads the file to the corporate server using techniques similar to the ones used to download the file.

// Negotiate a socket connection to the corporate FTP server

$corp_connection_id = ftp_connect(CORP_FTP_SERVER);

// Log in to the corporate server

if(!ftp_login($corp_connection_id, CORP_USERNAME, CORP_PASSWORD))

report_error_and_quit("Corporate ftp_login failed", $corp_connection_id);

// Move the destination directory

if(!ftp_chdir($corp_connection_id, CORP_DIRECTORY))

report_error_and_quit("Corporate ftp_chdir failed", $corp_connection_id);

// Upload the file

if(!ftp_put($corp_connection_id, CORP_FILE, "temp_file", FTP_ASCII))

report_error_and_quit("Corporate ftp_put failed", $corp_connection_id);

// Close connections to the corporate FTP server

ftp_close($corp_connection_id);

// Send notification that the webbot ran successfully

formatted_mail("ftpbot ran successfully at ".time("M d,Y h:s"), "", $mail_addr,

$content_type);

?>

Listing 13-5: Logging in and uploading the previously downloaded file to the corporate server

* * *

[41] The original document defining FTP can be viewed at http://www.w3.org/Protocols/rfc959.

[42] Katie Hafner and Matthew Lyon, Where Wizards Stay Up Late: The Origins of the Internet (New York: Simon & Schuster, 1996), 14.

[43] See Chapter 23 for information on how to make webbots run periodically.

PHP and FTP

PHP provides built-in functions that closely resemble standard FTP commands. In addition to transferring files, PHP allows your scripts to perform many administrative functions. Table 13-1 lists the most useful FTP commands supported by PHP.

Table 13-1. Common FTP Commands Supported by PHP

FTP Function (Where $ftp Is the FTP File Stream)

Usage

ftp_cdup($ftp);

Makes the parent directory the current directory

ftp_chdir ($ftp, "directory/path")

Changes the current

Return Main Page Previous Page Next Page

®Online Book Reader