PHP ftp_quit() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_quit() closes an FTP connection. It is an alias of ftp_close(), so both functions do exactly the same thing.

Use it when you are done uploading or downloading. Closing the connection frees the network resource and politely tells the server that you have finished.

Syntax

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

PHP
ftp_quit($ftp);
CallWhat it does
ftp_quit($ftp)Closes the connection and returns true on success.
ftp_quit($ftp) === ftp_close($ftp)Shows the two names are interchangeable, though you cannot close the same connection twice.
if (ftp_quit($ftp)) { ... }Runs code only after the connection was closed cleanly.

ftp_quit() is an alias of ftp_close(). Pick one name and use it everywhere in your project so the code stays easy to read. After closing, the $ftp variable cannot be used for any more FTP commands.

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

Parameters

ParameterDescription
ftpRequired. The FTP connection returned by ftp_connect() or ftp_ssl_connect().

Return Value

Return ValueDescription
boolReturns true on success or false on failure.

PHP Examples

Example 1 — Close the Connection

Open a connection, log in, and close it with ftp_quit() when you are done.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

echo "Connected", PHP_EOL;

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

How It Works

  1. ftp_connect() opens the control connection to the server.
  2. ftp_login() authenticates with a username and password.
  3. ftp_quit() sends the quit command and closes the connection.
  4. It returns true, so the success message is printed.

Example 2 — Do Not Use a Closed Connection

After ftp_quit(), the connection is finished. Wrap later calls in a check to avoid errors.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');

ftp_quit($ftp);
$ftp = null;

if ($ftp === null) {
    echo "No active connection, skipping commands", PHP_EOL;
}

How It Works

  1. ftp_quit() closes the session on the server.
  2. Setting $ftp to null makes it clear the variable no longer holds a usable connection.
  3. The if check stops the script from running more FTP functions on it.
  4. This avoids an error from using a closed connection.

Example 3 — Always Close in a finally Block

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');

try {
    if (!ftp_login($ftp, 'demo', 'secret')) {
        throw new Exception('Login failed');
    }

    echo "Folder: ", ftp_pwd($ftp), PHP_EOL;
} catch (Exception $e) {
    echo "Error: ", $e->getMessage(), PHP_EOL;
} finally {
    ftp_quit($ftp);
    echo "Closed", PHP_EOL;
}

How It Works

  1. The code inside try logs in and prints the current folder.
  2. If login fails, an exception is thrown and the catch block shows the message.
  3. The finally block always runs, whether there was an error or not.
  4. ftp_quit() inside finally guarantees the connection is closed.

Frequently Asked Questions

It closes an FTP connection. It is a simple alias of ftp_close(), so you can use either name.
Yes. They are aliases and behave the same way. ftp_close() is more common in tutorials, but ftp_quit() works equally well.
PHP closes the connection when the script ends, but it is better to close it yourself. This frees the server connection sooner and keeps your code tidy.
In PHP 8 you will get an error because the connection is closed. Create a new connection with ftp_connect() if you need to talk to the server again.
Yes. Connections made with ftp_connect() or ftp_ssl_connect() are both closed the same way.

Next: ftp_raw()

ftp_raw() sends a raw FTP command to the server and gives you the reply as an array of lin.

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