Log into an FTP server using PHP.

This is a beginner’s tutorial on how to log into to an FTP server using PHP. To do this, we will use PHP’s ftp_connect and ftp_login functions.

Let’s take a look at an example of an FTP login using PHP:

//The IP or hostname of the FTP server.
$ftpHost = '127.0.0.01';

//The FTP username.
$ftpUsername = 'test';

//The FTP password.
$ftpPassword = 'password';

//Attempt to connect to the FTP server by using
//PHP's ftp_connect server.
$ftpConnection = @ftp_connect($ftpHost);

//If ftp_connect returns a boolean FALSE value, then
//we failed to connect
if($ftpConnection === false){
    //Throw an exception
    throw new Exception('Could not connect to FTP server at ' . $ftpHost);
}

//If our FTP connection is successful, we can now attempt
//to login to the server.
//We can do this by using the ftp_login function.
$loggedIn = @ftp_login($ftpConnection, $ftpUsername, $ftpPassword);

//If ftp_login returns a boolean FALSE value, then we
//failed to login to the FTP server.
if($loggedIn === false){
    $phpErrMessage = error_get_last();
    if(!empty($phpErrMessage)){
        $phpErrMessage = $phpErrMessage['message'];
    }
    throw new Exception('Could not login to FTP server: ' . $phpErrMessage);
}

In the code snippet above, there are three important variables that you will need to change:

  • $ftpHost: This is the IP address or the host name of the FTP server in question.
  • $ftpUsername: You will need to change this to match your FTP username.
  • $ftpPassword: You will need to change this to match your FTP password.

Step 1: Connect with ftp_connect.

The example above attempts to connect to the server using PHP’s ftp_connect function. If ftp_connect returns a boolean FALSE value, then it means that an FTP connection could not be established. This can be because of a number of reasons, such as:

  1. The IP address or the host name is incorrect.
  2. The FTP server is not running.
  3. Your IP address has been blacklisted by the server.
  4. A firewall issue is preventing your code from connecting.

In our code, we throw an Exception if the FTP connection could not be established. As a result, our PHP script will terminate if the connection fails. This makes sense, as there is no point in attempting to login if an FTP connection could not be established.

If a valid connection can be made, then the ftp_connect function will return an FTP stream resource.

Step 2: Log into the FTP server using ftp_login.

Once the connection has been successful, we can attempt to log into the server by using PHP’s ftp_login function. This function takes in three parameters:

  1. The FTP stream that ftp_connect returned.
  2. Our FTP username.
  3. Our FTP password.

If the login attempt fails, then the ftp_login function will return a boolean FALSE value. By default, the function will also omit a warning message such as:

Warning: ftp_login(): Permission denied.

Note that in the ftp_login example above, we are intentionally suppressing this warning by using the @ symbol. Instead, we throw an Exception and include the warning message by utilizing the error_get_last function.

And that’s it. If your FTP server details are filled out correctly, then the above script should execute without any exceptions being thrown!

See also: