PHP ftp_close() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_close() closes an open FTP connection. Call it when you have finished all your FTP work.

It is an alias of ftp_quit(), so both functions do exactly the same thing. Closing the connection politely tells the server you are done and frees resources on both sides.

Syntax

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

PHP
ftp_close($ftp);
CallWhat it does
ftp_close($ftp)Ends the FTP session and returns true on success.
ftp_quit($ftp)The same function under its original name.
if ($ftp) { ftp_close($ftp); }Closes the connection only when it was opened successfully.

From PHP 8.1 the connection is an FTP\Connection object, not a resource. After ftp_close() you must not use the same connection variable again, because calling other FTP functions on it will throw an error.

Library declarations
PHP
ftp_close(FTP\Connection $ftp): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Close After Your Work Is Done

Open a connection, do a task, and close it. The host and login are placeholders.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

echo "Working in: ", ftp_pwd($ftp), PHP_EOL;

if (ftp_close($ftp)) {
    echo "Connection closed.", PHP_EOL;
}

How It Works

  1. We open the connection and log in.
  2. ftp_pwd() shows the server is ready and we are in the root.
  3. ftp_close() ends the session and returns true.
  4. The script prints a short confirmation.

Example 2 — Do Not Reuse a Closed Connection

After ftp_close(), using the same connection throws an error in PHP 8. Catch it to see what happens.

PHP
<?php
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "demo_user", "demo_pass");

ftp_close($ftp);

try {
    echo ftp_pwd($ftp);
} catch (Throwable $e) {
    echo get_class($e), ": connection is closed", PHP_EOL;
}

How It Works

  1. ftp_close() ends the FTP session.
  2. ftp_pwd() is then called on the closed connection.
  3. PHP 8 throws an error instead of returning false quietly.
  4. The catch block prints the error class so we can see what happened.

Example 3 — Always Close With a Helper

Use try and finally so the connection is closed even if something goes wrong in the middle.

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

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

try {
    ftp_login($ftp, "demo_user", "demo_pass");
    echo "Files: ", count(ftp_nlist($ftp, ".")), PHP_EOL;
} finally {
    ftp_close($ftp);
    echo "Closed safely.", PHP_EOL;
}

How It Works

  1. We stop early if ftp_connect() failed and returned false.
  2. Inside try we log in and count the files in the current folder.
  3. The finally block always runs, even when an exception is thrown.
  4. ftp_close() inside finally makes sure the connection is never left open.

Frequently Asked Questions

It closes an open FTP connection that you created with ftp_connect() or ftp_ssl_connect().
Yes. ftp_close() is an alias of ftp_quit(). They behave exactly the same, so use whichever name you prefer.
It is good practice. PHP closes the connection at the end of the script, but closing it yourself frees server resources sooner and makes the code clearer.
In PHP 8 it throws an error, because the FTP\Connection object is no longer valid. Open a new connection if you need to keep working.
An FTP\Connection object. Before PHP 8.1 it was an FTP stream resource, but the way you pass it is the same.

Next: ftp_connect()

ftp_connect() opens a plain FTP connection to a server. It is always the first step before.

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