Specifications
Using FTP to Back Up or Mirror a File
The FTP functions are useful for moving and copying files from and to other hosts. One com-
mon use you might make of this is to back up your Web site or mirror files at another location.
We will look at a simple example using the FTP functions to mirror a file. This script is shown
in Listing 17.4.
LISTING 17.4 ftpmirror.php—Script to Download New Versions of a File from an FTP
Server
<html>
<head>
<title>Mirror update</title>
</head>
<body>
<h1>Mirror update</h1>
<?
// set up variables - change these to suit application
$host = “ftp.cs.rmit.edu.au”;
$user = “anonymous”;
$password = “laura@tangledweb.com.au”;
$remotefile = “/pub/tsg/ttssh14.zip”;
$localfile = “$DOCUMENT_ROOT/../writable/ttssh14.zip”;
// connect to host
$conn = ftp_connect(“$host”);
if (!$conn)
{
echo “Error: Could not connect to ftp server<br>”;
exit;
}
echo “Connected to $host.<br>”;
// log in to host
@ $result = ftp_login($conn, $user, $pass);
if (!$result)
{
echo “Error: Could not log on as $user<br>”;
ftp_quit($conn);
exit;
}
echo “Logged in as $user<br>”;
// check file times to see if an update is required
Using Network and Protocol Functions
C
HAPTER 17
17
USING NETWORK
AND
PROTOCOL
FUNCTIONS
379
22 7842 CH17 3/6/01 3:39 PM Page 379










