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);
Call
What 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
Parameter
Description
hostname
Required. The server address, such as ftp.example.com. Do not add ftp:// or a trailing slash.
port
Optional. The port to connect to. The default is 21.
timeout
Optional. The timeout in seconds for network operations. The default is 90.
Return Value
Return Value
Description
FTP\Connection|false
An 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);
Output
Connected and logged in.
Type: FTP\Connection
How It Works
ftp_connect() opens the network connection on port 21 with a 30 second timeout.
We check for false to handle a failed connection safely.
ftp_login() sends the username and password.
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);
}
Output
Connection failed. Check the host and port.
How It Works
The host name does not exist, and the timeout is only 5 seconds.
ftp_connect() returns false because it cannot reach any server.
The @ operator hides the PHP warning so we can show our own message.
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.
The function returns a nullable FTP\Connection so callers can check for null.
If ftp_connect() fails we return null at once.
If ftp_login() fails we close the connection and return null.
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.