ftp_connect()
The ftp_connect() function is a PHP built-in function that is used to establish an FTP connection to a remote server. The function takes two parameters:
The PHP ftp_connect() Function
ftp_connect() opens a control connection to an FTP server and returns a connection handle that every other FTP function (ftp_login(), ftp_get(), ftp_put(), …) needs as its first argument. Think of it as dialing the server: the call only opens the channel — it does not authenticate you. You still have to log in with ftp_login() before you can transfer files.
This page covers the syntax, the three parameters, a complete connect-login-close workflow, how to handle failures, and how ftp_connect() relates to its secure sibling, ftp_ssl_connect().
Syntax
ftp_connect(string $hostname, int $port = 21, int $timeout = 90): FTP\Connection|false| Parameter | Required | Default | Description |
|---|---|---|---|
$hostname | yes | — | Hostname or IP address of the FTP server. Do not include the ftp:// scheme or a trailing slash — pass ftp.example.com, not ftp://ftp.example.com/. |
$port | no | 21 | TCP port of the control connection. Plain FTP uses 21; change it only when the server listens elsewhere. |
$timeout | no | 90 | Seconds to wait for network operations on this connection before giving up. |
Return value. On success you get a connection object — an FTP\Connection instance as of PHP 8.1, or a resource in earlier versions. On failure it returns false, so the result must always be checked before use.
Note:
ftp_connect()establishes an unencrypted connection — credentials and data travel in clear text. For anything over the public internet, preferftp_ssl_connect()(FTPS) instead.
A complete connect → login → close workflow
A typical session opens the connection, authenticates, does its work, then closes the handle:
<?php
// 1. Open the control connection (does not log you in)
$conn = ftp_connect('ftp.example.com', 21, 30);
// 2. Authenticate
ftp_login($conn, 'username', 'password');
// 3. Many servers behind NAT/firewalls need passive mode
ftp_pasv($conn, true);
// 4. Do some work — e.g. upload a file
ftp_put($conn, 'backup.sql', 'local-backup.sql', FTP_BINARY);
// 5. Always release the connection
ftp_close($conn);Each step maps to a dedicated function: ftp_login() authenticates, ftp_pasv() toggles passive mode, ftp_put() and ftp_get() transfer files, and ftp_close() tears the connection down.
Handling a failed connection
Because ftp_connect() returns false on failure, treat a missing handle as a hard stop — calling later FTP functions with false triggers a TypeError. Check both the connection and the login:
<?php
$conn = ftp_connect('ftp.example.com', 21, 30);
if ($conn === false) {
// Wrong host, blocked port 21, or DNS/network failure
exit("Could not reach the FTP server.\n");
}
if (!ftp_login($conn, 'username', 'password')) {
ftp_close($conn);
exit("Login failed — check the username and password.\n");
}
echo "Connected and authenticated.\n";
// ... transfer files ...
ftp_close($conn);Use a strict === false comparison rather than !$conn. The two behave the same for ftp_connect(), but === false makes your intent explicit and avoids surprises when a function can legitimately return an empty-but-truthy value.
Common gotchas
- The FTP extension may be disabled.
ftp_*functions require PHP compiled with--enable-ftp(or thephp-ftppackage). Guard withif (!function_exists('ftp_connect')) { ... }if portability matters. - Passive mode. If
ftp_connect()andftp_login()succeed but listings or transfers hang, the server is likely behind a firewall — switch to passive mode withftp_pasv($conn, true)after logging in. - Connecting is not authenticating. A returned handle only means the TCP channel is open. A wrong password fails later, at
ftp_login(). - No
ftp://prefix. Pass a bare hostname; the scheme is implied by the function.
For an overview of the whole FTP toolkit, see the PHP FTP functions reference.