W3docs

ftp_ssl_connect()

The ftp_ssl_connect() function is a built-in PHP function that establishes a secure SSL-encrypted connection to an FTP server. In this article, we'll discuss

Understanding the PHP Function ftp_ssl_connect()

The ftp_ssl_connect() function opens an explicit FTPS (FTP over SSL/TLS) connection to an FTP server. It is the secure counterpart of ftp_connect(): both return a connection handle you pass to the other ftp_* functions, but ftp_ssl_connect() upgrades the control channel to an encrypted TLS session so your login credentials and commands are not sent in plaintext.

This page covers what the function does, its parameters and return value, a full working example, and the common pitfalls (FTPS vs. SFTP, certificate checks, and passive mode).

What is ftp_ssl_connect()?

The ftp_ssl_connect() function establishes a secure SSL/TLS-encrypted connection to an FTP server. It requires PHP to be built with the ftp extension and the openssl extension. It accepts three parameters:

  1. host: The name of the FTP server to connect to.
  2. port: The port number to connect to. This parameter is optional and defaults to port 21.
  3. timeout: The timeout for the connection attempt in seconds. This parameter is optional and defaults to 90 seconds.

Note: By default, PHP verifies SSL certificates using the system's CA bundle. For production environments, ensure your CA certificates are up to date to prevent connection failures.

The function returns an FTP connection handle on success (an FTP\Connection object in PHP 8.1+, or a resource in earlier versions) and false on failure.

FTPS vs. SFTP — pick the right one

These two are often confused because both transfer files securely, but they are unrelated protocols:

  • FTPS (what this function speaks) is the FTP protocol wrapped in TLS. Use ftp_ssl_connect() and the rest of the ftp_* family.
  • SFTP is a subsystem of SSH and has nothing to do with FTP. PHP cannot reach it through the ftp_* functions — you use the ssh2_* extension or a library such as phpseclib instead.

If your server only offers SSH/SFTP, ftp_ssl_connect() will not connect to it.

Syntax of ftp_ssl_connect()

The syntax of the ftp_ssl_connect() function is as follows:

Syntax of ftp_ssl_connect()

resource ftp_ssl_connect ( string $host [, int $port = 21 [, int $timeout = 90 ]] )

The ftp_ssl_connect() function takes three parameters, all of which are optional except for the host parameter. The port parameter specifies the port number to connect to, and the timeout parameter specifies the timeout for the connection attempt in seconds.

Usage of ftp_ssl_connect()

To use the ftp_ssl_connect() function, you simply need to call the function and pass in the required parameters. Here's an example:

Usage of ftp_ssl_connect()

<?php

// Set up a secure SSL-encrypted FTP connection
$conn = ftp_ssl_connect('ftp.example.com', 21, 60);

// Login with your FTP credentials
ftp_login($conn, 'username', 'password');

// Perform FTP operations

// Close the FTP connection
ftp_close($conn);

In this example, we establish a secure SSL-encrypted connection to the FTP server using the ftp_ssl_connect() function. Then we log in with our FTP credentials using the ftp_login() function. After logging in, we can perform any necessary FTP operations. Finally, we close the FTP connection using the ftp_close() function.

A complete, production-ready upload

The minimal example above skips the checks you actually need in real code. The version below verifies that the connection and login succeeded, switches to passive mode (required behind most firewalls and NAT), and uploads a file:

Complete FTPS upload

<?php

$host = 'ftp.example.com';
$user = 'username';
$pass = 'password';

// 1. Open the secure connection (false on failure)
$conn = ftp_ssl_connect($host, 21, 30);
if ($conn === false) {
    exit("Could not connect to {$host}\n");
}

// 2. Authenticate
if (!ftp_login($conn, $user, $pass)) {
    ftp_close($conn);
    exit("Login failed for {$user}\n");
}

// 3. Passive mode — almost always required for FTPS behind a firewall
ftp_pasv($conn, true);

// 4. Upload a local file in binary mode
if (ftp_put($conn, 'remote/report.csv', 'local/report.csv', FTP_BINARY)) {
    echo "Upload succeeded\n";
} else {
    echo "Upload failed\n";
}

// 5. Always close the connection
ftp_close($conn);

The key habits here — checking the return value of every call and enabling passive mode with ftp_pasv() — are what make FTPS reliable in practice. Without passive mode, data transfers frequently hang behind firewalls because the server tries to open a return connection the client cannot accept.

Error handling in ftp_ssl_connect()

It's important to handle errors properly when using the ftp_ssl_connect() function. If the function returns false, it means that the operation was unsuccessful. Here's an example of how to handle errors:

Error handling in ftp_ssl_connect()

<?php

$conn = ftp_ssl_connect('ftp.example.com');

if ($conn === false) {
    echo "Failed to establish a secure SSL-encrypted connection to the FTP server.\n";
} else {
    // Perform FTP operations

    // Close the FTP connection
    ftp_close($conn);
}

By handling errors appropriately and checking the return value of the function, you can ensure the success of your FTP operations using the ftp_ssl_connect() function.

Common pitfalls

  • A returned handle does not mean you are logged in. ftp_ssl_connect() only opens the encrypted channel; you still need a successful ftp_login() before any operation works.
  • Forgetting passive mode. Most data transfers stall behind firewalls or NAT unless you call ftp_pasv($conn, true) after login.
  • Confusing FTPS with SFTP. As noted above, this function cannot talk to an SSH/SFTP server.
  • Check that the function exists. It is only available when PHP is compiled with both the ftp and openssl extensions. Guard with function_exists('ftp_ssl_connect') if you cannot guarantee the build.
  • Implicit FTPS (port 990) is not supported. ftp_ssl_connect() performs explicit FTPS only. For implicit FTPS, use a stream wrapper or a dedicated library instead.

Conclusion

The ftp_ssl_connect() function is the secure entry point to PHP's FTP toolkit, opening an explicit FTPS connection so credentials and commands travel over TLS instead of plaintext. Pair it with a checked ftp_login(), enable passive mode, and verify every return value, and you have a reliable, secure file-transfer workflow. When the remote server speaks SSH/SFTP rather than FTPS, reach for the ssh2_* extension instead.

Practice

Practice
What is the function of ftp_ssl_connect() in PHP?
What is the function of ftp_ssl_connect() in PHP?
Was this page helpful?