Specifications
useful result from the function. In this case, we choose to artificially set the $remotetime vari-
able to be “newer” than the $localtime variable by adding 1 to it. This will ensure that an
attempt is made to download the file:
if (!($remotetime >= 0))
{
// This doesn’t mean the file’s not there, server may not support mod time
echo “Can’t access remote file time.<br>”;
$remotetime=$localtime+1; // make sure of an update
}
else
{
echo “Remote file last updated “;
echo date(“G:i j-M-Y”, $remotetime);
echo “<br>”;
}
When we have both times, we can compare them to see whether we need to download the file
or not:
if (!($remotetime > $localtime))
{
echo “Local copy is up to date.<br>”;
exit;
}
Downloading the File
At this stage we will try to download the file from the server:
echo “Getting file from server...<br>”;
$fp = fopen ($localfile, “w”);
if (!$success = ftp_fget($conn, $fp, $remotefile, FTP_BINARY))
{
echo “Error: Could not download file”;
fclose($fp);
ftp_quit($conn);
exit;
}
fclose($fp);
echo “File downloaded successfully”;
We open a local file using fopen() as we have seen previously. After we have done this, we
call the function ftp_fget(), which attempts to download the file and store in a local file. This
function takes four parameters. The first three are straightforward—the FTP connection, the
local file handle, and the path to the remote file. The fourth parameter is the FTP mode.
Advanced PHP Techniques
P
ART IV
384
22 7842 CH17 3/6/01 3:39 PM Page 384










