Create a connection using FileTransferFTP
The code used to create an FTP connection is similar to the following one.

try {
  $ftp = FileTransferFTP::factory($jail, $settings);
  $ftp-&gt;connect();
  // Copy the files.
}
catch(Exception $e) {
  // An error happened.
}


$jail is the directory to which the operations are restricted. Any tentative to write files externally to this directory will raise a FileTransferException exception.

$settings contains:


$settings['username']: The username
$settings['password']: The password
$settings['advanced']['hostname']: The hostname for the FTP server (default to localhost)
$settings['advanced']['port']: The server port (default to 21) 


FileTransferFTP::factory() returns the instance of the sub-class that takes care of the file transfer, if there is one, or raises an exception, if there aren't sub-classes.

The code currently executed from FileTransferFTP::factory() in Drupal 7 is the following one.

  $username = empty($settings['username']) ? '' : $settings['username'];
  $password = empty($settings['password']) ? '' : $settings['password'];
  $hostname = empty($settings['advanced']['hostname']) ? 'localhost' : $settings['advanced']['hostname'];
  $port = empty($settings['advanced']['port']) ? 21 : $settings['advanced']['port'];

  if (function_exists('ftp_connect')) {
    $class = 'FileTransferFTPExtension';
  }
  else {
    throw new FileTransferException('No FTP backend available.');
  }

  return new $class($jail, $username, $password, $hostname, $port);


Future versions of the class could implement the method differently, and create different instances of the FileTransferChmodInterface interface basing on different conditions.

In Drupal 8, the class has been renamed Drupal\Core\FileTransfer\FTP.