Specifications
The basic steps we follow in this script are the same as if you wanted to manually FTP the file
from a command line interface:
1. Connect to the remote FTP server.
2. Log in (either as a user or anonymous).
3. Check whether the remote file has been updated.
4. If it has, download it.
5. Close the FTP connection.
Let’s take each of these in turn.
Connecting to the Remote FTP Server
This step is equivalent to typing
ftp hostname
at a command prompt on either a Windows or UNIX platform. We accomplish this step in PHP
with the following code:
$conn = ftp_connect(“$host”);
if (!$conn)
{
echo “Error: Could not connect to ftp server<br>”;
exit;
}
echo “Connected to $host.<br>”;
The function call here is to ftp_connect(). This function takes a hostname as parameter, and
returns either a handle to a connection, or false if a connection could not be established. The
function can also takes the port number on the host to connect to as an optional second para-
meter. (We have not used this here.) If you don’t specify a port number, it will default to port
21, the default for FTP.
Logging In to the FTP Server
The next step is to log in as a particular user with a particular password. You can achieve this
using the ftp_login() function:
@ $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>”;
Advanced PHP Techniques
P
ART IV
382
22 7842 CH17 3/6/01 3:39 PM Page 382










