Specifications

The function takes three parameters: an FTP connection (obtained from ftp_connect()), a
username, and a password. It will return true if the user can be logged in, and false if he
cant. You will notice that we put an @ symbol at the start of the line to suppress errors. We do
this because, if the user cannot be logged in, you will get a PHP warning in your browser win-
dow. You can catch the error as we have done here by testing $result, and supplying your
own, more user-friendly error message.
Notice that if the login attempt fails, we actually close the FTP connection using
ftp_quit()more on this in a minute.
Checking File Update Times
Given that we are updating a local copy of a file, it is sensible to check whether the file needs
updating first because you dont want to have to re-download a file, particularly a large one, if
its up to date. This will avoid unnecessary network traffic. Lets look at the code that does
this.
First, we check that we have a local copy of the file, using the
file_exists() function. If we
dont then obviously we need to download the file. If it does exist, we get the last modified
time of the file using the filemtime() function, and store it in the $localtime variable. If it
doesnt exist, we set the $localtime variable to 0 so that it will be older than any possible
remote file modification time:
echo “Checking file time...<br>”;
if (file_exists($localfile))
{
$localtime = filemtime($localfile);
echo “Local file last updated “;
echo date(“G:i j-M-Y”, $localtime);
echo “<br>”;
}
else
$localtime=0;
(You can read more about the file_exists() and filemtime() functions in Chapter 2 and
Chapter 16, Interacting with the File System and the Server, respectively.)
After we have sorted out the local time, we need to get the modification time of the remote
file. You can get this using the ftp_mdtm() function:
$remotetime = ftp_mdtm($conn, $remotefile);
This function takes two parametersthe FTP connection handle, and the path to the remote
fileand returns either the UNIX time stamp of the time the file was last modified, or -1 if
there is an error of some kind. Not all FTP servers support this feature, so we might not get a
Using Network and Protocol Functions
C
HAPTER 17
17
USING NETWORK
AND
PROTOCOL
FUNCTIONS
383
22 7842 CH17 3/6/01 3:39 PM Page 383