PHP ftp_connect() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_connect() opens a plain FTP connection to a server. It is always the first step before you log in or transfer any files.

The function only makes the network connection. You still need to call ftp_login() with a username and password afterwards. For an encrypted connection, use ftp_ssl_connect() instead.

Syntax

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

PHP
$ftp = ftp_connect("ftp.example.com", 21, 30);
CallWhat it does
ftp_connect("ftp.example.com")Connects on the default port 21 with the default 90 second timeout.
ftp_connect("ftp.example.com", 2121)Connects to a server that uses a custom port.
ftp_connect("ftp.example.com", 21, 10)Gives up after 10 seconds if the server does not answer.

From PHP 8.1 ftp_connect() returns an FTP\Connection object (it returned a resource before). It returns false when the connection fails, so always check the result before using it.

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

Parameters

ParameterDescription
hostnameRequired. The server address, such as ftp.example.com. Do not add ftp:// or a trailing slash.
portOptional. The port to connect to. The default is 21.
timeoutOptional. The timeout in seconds for network operations. The default is 90.

Return Value

Return ValueDescription
FTP\Connection|falseAn FTP\Connection object on success, or false if the connection fails.

PHP Examples

Example 1 — Connect and Log In

Open a connection, log in, and print the result. The host and login are placeholders.

PHP
<?php
$ftp = ftp_connect("ftp.example.com", 21, 30);

if ($ftp === false) {
    exit("Could not connect to the server.");
}

if (ftp_login($ftp, "demo_user", "demo_pass")) {
    echo "Connected and logged in.", PHP_EOL;
    echo "Type: ", get_class($ftp), PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_connect() opens the network connection on port 21 with a 30 second timeout.
  2. We check for false to handle a failed connection safely.
  3. ftp_login() sends the username and password.
  4. get_class() shows that PHP 8. 1+ gives us an FTP\Connection object.

Example 2 — Connection Failure Returns false

If the host is wrong or the server is down, ftp_connect() returns false and also emits a warning.

PHP
<?php
$ftp = @ftp_connect("no-such-host.invalid", 21, 5);

if ($ftp === false) {
    echo "Connection failed. Check the host and port.", PHP_EOL;
} else {
    echo "Connected.", PHP_EOL;
    ftp_close($ftp);
}

How It Works

  1. The host name does not exist, and the timeout is only 5 seconds.
  2. ftp_connect() returns false because it cannot reach any server.
  3. The @ operator hides the PHP warning so we can show our own message.
  4. We compare with === false so only a real failure triggers the message.

Example 3 — Reusable Connect Helper

Put the connect and login steps in one function that returns a ready connection or null.

PHP
<?php
function openFtp(string $host, string $user, string $pass): ?FTP\Connection
{
    $ftp = @ftp_connect($host, 21, 15);

    if ($ftp === false) {
        return null;
    }

    if (!@ftp_login($ftp, $user, $pass)) {
        ftp_close($ftp);
        return null;
    }

    return $ftp;
}

$ftp = openFtp("ftp.example.com", "demo_user", "demo_pass");

echo $ftp ? "Ready to use" : "Unable to connect", PHP_EOL;

How It Works

  1. The function returns a nullable FTP\Connection so callers can check for null.
  2. If ftp_connect() fails we return null at once.
  3. If ftp_login() fails we close the connection and return null.
  4. On success the ready connection is returned and used by the caller.

Frequently Asked Questions

It opens a plain FTP connection to a server and returns a connection object. You must call ftp_login() afterwards before sending commands.
It returns an FTP\Connection object on success or false on failure. Before PHP 8.1 it returned a resource.
No. Pass only the host name or IP address, like ftp.example.com. Adding ftp:// makes the connection fail.
ftp_connect() uses plain FTP, which is not encrypted. ftp_ssl_connect() starts an FTP over SSL/TLS session, which protects your username, password and data.
It sets how many seconds PHP waits for network operations before giving up. The default is 90. Use a smaller value if you want faster failure.

Next: ftp_delete()

ftp_delete() removes a file from the FTP server. It is the remote version of unlink().

ftp_delete() 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