PHP Function Reference

PHP ftp_ssl_connect() Function



The PHP ftp_ssl_connect() function opens an explicit secure SSL-FTP connection to the specified hostname. This function will succeed even if the server is not configured for SSL-FTP, or its certificate is invalid.

Note: Before PHP 7.0.0, this function was only available if both the ftp module and the OpenSSL support have been built statically into PHP.

Syntax

ftp_ssl_connect(hostname, port, timeout)

Parameters

hostname Required. Specify the FTP server address. This parameter should not be prefixed with ftp:// or contain any trailing slashes.
port Optional. Specify an alternate port to connect to. If it is omitted or set to zero, then the default FTP port, 21, will be used.
timeout Optional. Specify the timeout in seconds for all subsequent network operations. Default is 90 seconds. The timeout can be changed and queried at any time with ftp_set_option() and ftp_get_option().

Return Value

Returns a SSL-FTP stream on success or false on error.

Example:

The example below shows the usage of ftp_ssl_connect() function.

<?php
//FTP server to use
$ftp_server = "ftp.example.com";

//username for the FTP Connection
$ftp_user = "user";
  
//password for the user
$ftp_pass = "password";

//set up a SSL-FTP connection
$ftp = ftp_ssl_connect($ftp_server);
   
if($ftp) {
  echo "Successfully connected to $ftp_server!\n";
 
  //trying to login
  if(@ftp_login($ftp, $ftp_user, $ftp_pass)) {
    echo "Connected as $ftp_user@$ftp_server\n";
  } else {
    echo "Couldn't connect as $ftp_user\n";
  }

  //close the connection
  if(ftp_close($ftp)) {
    echo "Connection closed successfully!\n"; 
  } 
}
?>

The output of the above code will be:

Successfully connected to ftp.example.com!
Connected as user@ftp.example.com
Connection closed successfully!

❮ PHP FTP Reference