ftp_close()
The ftp_close() function is a PHP built-in function that is used to close an FTP connection. The function takes a connection identifier returned by the
What ftp_close() Does
ftp_close() closes an active FTP connection that you previously opened with ftp_connect() (or ftp_ssl_connect()). It frees the network socket and the server-side session so neither side keeps the connection open longer than necessary.
You call it once you have finished transferring files — after any ftp_get(), ftp_put(), or ftp_nlist() operations. The function returns true on success and false on failure.
PHP closes the FTP connection automatically when the script ends, so a forgotten
ftp_close()will not leak a socket forever. Calling it explicitly still matters in long-running scripts (workers, queue consumers, loops that open many connections), where you want to release the server session immediately instead of waiting for the script to terminate.
Syntax
ftp_close(FTP\Connection $ftp): bool| Parameter | Description |
|---|---|
$ftp | The connection handle returned by ftp_connect() or ftp_ssl_connect(). |
Since PHP 8.1 the handle is an
FTP\Connectionobject. In PHP 8.0 and earlier it was aresource. You use it the same way in both versions — you never inspect it directly, you just pass it to theftp_*functions.
Basic Usage
A complete connect → log in → work → close cycle looks like this:
<?php
// Open a connection to the FTP server
$ftp = ftp_connect('ftp.example.com');
// Authenticate
ftp_login($ftp, 'username', 'password');
// Download a file
ftp_get($ftp, 'local-copy.txt', 'remote-file.txt', FTP_BINARY);
// Always close the connection when you are done
ftp_close($ftp);The key idea: every ftp_connect() should be paired with an ftp_close(), just like every opened file should be closed.
Checking the Return Value
ftp_close() returns a boolean, so you can confirm the connection was released cleanly:
<?php
if (ftp_close($ftp)) {
echo "Connection closed successfully.\n";
} else {
echo "Failed to close the connection.\n";
}Closing Safely with try/finally
If an operation between connecting and closing throws or exits early, the ftp_close() line may never run. Wrapping the work in try/finally guarantees the connection is closed even when an error occurs:
<?php
$ftp = ftp_connect('ftp.example.com');
try {
ftp_login($ftp, 'username', 'password');
ftp_get($ftp, 'local.txt', 'remote.txt', FTP_BINARY);
// ... more operations that might fail ...
} finally {
// Runs whether the try block succeeded or threw
ftp_close($ftp);
}This is the recommended pattern for production code: the finally block is the single place responsible for releasing the connection.
Common Mistakes
- Using the handle after closing it. Once
ftp_close()runs, the handle is invalid. Any laterftp_*call on it will fail. - Passing an invalid handle. If
ftp_connect()failed it returnsfalse, not a connection. Callingftp_close(false)raises aTypeErrorin PHP 8. Check the connection before using it. - Calling it without a parameter.
ftp_close()requires the connection handle; there is no default.
Summary
ftp_close() ends an FTP session opened by ftp_connect() and returns true on success. Although PHP closes the connection at the end of the script, calling ftp_close() explicitly — ideally inside a finally block — keeps long-running scripts tidy and releases the server session as soon as your work is done. To explore the rest of the workflow, see ftp_connect(), ftp_login(), and ftp_put().