PHP ftp_ssl_connect() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_ssl_connect() opens a secure FTP connection over SSL/TLS. It is the secure version of ftp_connect().

The connection is encrypted, so your username, password and files are protected while they travel over the network. This function is only available when PHP was built with OpenSSL support.

Syntax

Call ftp_ssl_connect() with the values you need. Start with this line.

PHP
$ftp = ftp_ssl_connect('ftp.example.com', 21, 30);
CallWhat it does
ftp_ssl_connect('ftp.example.com')Opens a secure connection on the default port 21 with a 90 second timeout.
ftp_ssl_connect('ftp.example.com', 21, 30)Uses a 30 second timeout for network operations.
function_exists('ftp_ssl_connect')Checks that your PHP has OpenSSL support before you call the function.

ftp_ssl_connect() needs OpenSSL. If the function does not exist, your PHP build was made without it, and you should enable OpenSSL in php.ini or your hosting panel. You still log in with ftp_login() after connecting. This is explicit FTPS, not SFTP, which is a different protocol.

Library declarations
PHP
ftp_ssl_connect(string $hostname, int $port = 21, int $timeout = 90): FTP\Connection|false

Parameters

ParameterDescription
hostnameRequired. The FTP server address, such as ftp.example.com. Do not add ftp:// at the start.
portOptional. The port of the FTP server. The default is 21.
timeoutOptional. The timeout in seconds for network operations. The default is 90.

Return Value

Return ValueDescription
FTP\Connection|falseReturns an FTP\Connection object on success or false on failure.

PHP Examples

Example 1 — Open a Secure Connection

Connect to a server over SSL/TLS and log in.

PHP
<?php
$ftp = ftp_ssl_connect('ftp.example.com');

if ($ftp === false) {
    die("Could not connect securely");
}

if (ftp_login($ftp, 'demo', 'secret')) {
    echo "Secure login successful", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_ssl_connect() opens an encrypted connection to the server.
  2. If it returns false, the script stops with a clear message.
  3. ftp_login() then sends the username and password over the encrypted channel.
  4. ftp_close() ends the session when we are done.

Example 2 — Check That OpenSSL Is Available

Before using ftp_ssl_connect(), confirm the function exists so you can fall back or show a helpful message.

PHP
<?php
if (!function_exists('ftp_ssl_connect')) {
    echo "OpenSSL support is missing in this PHP build", PHP_EOL;
    exit;
}

$ftp = ftp_ssl_connect('ftp.example.com', 21, 30);

echo $ftp ? "Connected securely" : "Connection failed", PHP_EOL;

if ($ftp) {
    ftp_close($ftp);
}

How It Works

  1. function_exists() tells you if PHP was built with OpenSSL support.
  2. If the function is missing, the script prints a message and stops.
  3. Otherwise ftp_ssl_connect() tries to connect with a 30 second timeout.
  4. The result is checked before ftp_close() so we never close a false value.

Example 3 — Secure Upload with a Longer Timeout

Open a secure connection, raise the timeout, and upload a file in passive mode.

PHP
<?php
$ftp = ftp_ssl_connect('ftp.example.com', 21, 60);
ftp_login($ftp, 'demo', 'secret');
ftp_pasv($ftp, true);

ftp_set_option($ftp, FTP_TIMEOUT_SEC, 120);

if (ftp_put($ftp, 'backup.zip', __DIR__ . '/backup.zip', FTP_BINARY)) {
    echo "Secure upload finished", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_ssl_connect() gives us an encrypted connection with a 60 second timeout.
  2. ftp_pasv() turns on passive mode for the data connection.
  3. ftp_set_option() raises the timeout to 120 seconds for the big file.
  4. ftp_put() uploads backup. zip safely in binary mode.

Frequently Asked Questions

It opens a secure FTP connection using SSL/TLS. It works like ftp_connect(), but the traffic is encrypted.
PHP was built without OpenSSL. Enable the OpenSSL extension in php.ini or ask your host to turn it on.
No. ftp_ssl_connect() is FTPS, which is FTP with TLS. SFTP uses the SSH protocol and needs the ssh2 extension or a library like phpseclib.
Yes. ftp_ssl_connect() only opens the secure channel. You must call ftp_login() with your username and password afterwards.
It returns false. Always check the result before you use the connection in other FTP functions.

Next: ftp_systype()

ftp_systype() returns the operating system type of the remote FTP server, such as UNIX.

ftp_systype() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful