Specifications
There are two modes for an FTP transfer, ASCII and binary. The ASCII mode is used for trans-
ferring text files (that is, files that consist solely of ASCII characters), and the binary mode,
used for transferring everything else. PHP’s FTP library comes with two predefined constants,
FTP_ASCII and FTP_BINARY, that represent these two modes. You need to decide which mode
fits your file type, and pass the corresponding constant to ftp_fget() as the fourth parameter.
In this case we are transferring a zip file, and so we have used the FTP_BINARY mode.
The ftp_fget() function returns true if all goes well, or false if an error is encountered. We
store the result in $success, and let the user know how it went.
After the download has been attempted, we close the local file using the fclose() function.
As an alternative to ftp_fget(), we could have used ftp_get(), which has the following
prototype:
int ftp_get (int ftp_connection, string localfile_path,
string remotefile_path, int mode)
This function works in much the same way as ftp_fget(), but does not require the local file
to be open. You pass it the system filename of the local file you would like to write to rather
than a file handle.
Note that there is no equivalent to the FTP command mget, which can be used to download
multiple files at a time. You must instead make multiple calls to ftp_fget() or ftp_get().
Closing the Connection
After we have finished with the FTP connection, you should close it using the ftp_quit()
function:
ftp_quit($conn);
You should pass this function the handle for the FTP connection.
Uploading Files
If you want to go the other way, that is, copy files from your server to a remote machine, you
can use two functions that are basically the opposite of ftp_fget() and ftp_get(). These
functions are called ftp_fput() and ftp_put(). They have the following prototypes:
int ftp_fput (int ftp_connection, string remotefile_path, int fp, int mode)
int ftp_put (int ftp_connection, string remotefile_path,
string localfile_path, int mode)
The parameters are the same as for the _get equivalents.
Using Network and Protocol Functions
C
HAPTER 17
17
USING NETWORK
AND
PROTOCOL
FUNCTIONS
385
22 7842 CH17 3/6/01 3:39 PM Page 385










