Building An FTP Client With PHP - PHP and FTP 101
(Page 2 of 7 )
PHP has 25 functions that allow us to work with FTP servers. Transferring files with FTP is the most common way to copy/move files from one server to another. It's also one of the quickest and easiest methods.
As with any protocol, a connection to the remote server is required before commands can be sent to it. With PHP, we use the ftp_connect function. Its signature is shown below:
resource ftp_connect ( string host [, int port [, int timeout]]) Ftp_connect accept one compulsory and two optional parameters:
- host: The IP address, domain name or NetBIOS name of the remote host to which we are requesting the FTP connection.
- port: An optional parameter which can be used to specify the port to attempt to connect to. If this is left empty, then the default FTP port of 21 is used.
- timeout: An optional parameter which can be used to specify the number of seconds to waiting before a connection is deemed to have “timed out”. If not specified, the default value of 90 seconds will be used.
Ftp_connect returns a resource variable which holds the details of the newly established FTP connection. This variable then needs to be passed to the ftp_login function along with a username and password to authenticate ourselves against the remote FTP server.
The ftp_login functions signature looks like this:
bool ftp_login ( resource ftp_stream, string username, string password) Putting the ftp_connect and ftp_login functions together, we can connect to and authenticate with a remote FTP server with the following PHP code:
<?php
$conn = @ftp_connect("my.ftpserver.com")
or die("Couldn't connect to FTP server");
$login = @ftp_login($conn, "username", "password")
or die("Login credentials were rejected");
?> Notice how I've used the @ symbol prepended to the calls to ftp_connect and ftp_login? This stops any errors from being output to the client, and instead we use the die function to terminate with an error message if either ftp_connect or ftp_login fail.
Once connected to the FTP server, it's common to retrieve the name of the current directory that we are working with and also to display the files and directories which that directory contains. The ftp_pwd function returns the current directory name, and its signature looks like this:
string ftp_pwd ( resource ftp_stream) As you can probably guess, we need to pass the connection resource created from the call to ftp_connect to ftp_pwd. Building on our last example, we can connect to the Microsoft FTP site and show the current working directory like this:
<?php
$ftpServer = "ftp.microsoft.com";
$ftpUser = "anonymous";
$ftpPass = "me@myhost.com";
set_time_limit(160);
$conn = @ftp_connect($ftpServer)
or die("Couldn't connect to FTP server");
$login = @ftp_login($conn, $ftpUser, $ftpPass)
or die("Login credentials were rejected");
$workingDir = ftp_pwd($conn);
echo "You are in the directory: $workingDir";
ftp_quit($conn);
?> The highlighted code is new. I've defined the FTP server, user and password as variables and I've also used set_time_limit to extend the time that PHP should try to connect from 90 seconds to 160 seconds.
As the code above shows, we use ftp_pwd to get the current working directory on the FTP server as the $workingDir variable. We then echo this to the screen, and call ftp_quit to close the FTP connection.
The output from the script is simple and looks like this:
You are in the directory: /
Now that we understand the basics of connecting to, logging into, and retrieving the current working directory from a remote FTP server, let's look at how to retrieve files, how to change directories, etc.
Next: Retrieving a File Listing >>
More PHP Articles
More By Mitchell Harper