W3docs

ftp_quit()

The ftp_quit() function is a built-in PHP function that closes an FTP connection. In this article, we'll discuss the function in detail and provide you with a

Understanding the PHP Function ftp_quit()

The ftp_quit() function closes an open FTP connection and releases the resources associated with it. It is the counterpart of ftp_connect(): once you have finished transferring files, calling ftp_quit() sends the FTP QUIT command to the server and tears down the session cleanly.

This article explains what the function does, why closing a connection matters, and how to use it safely in older PHP codebases — along with the modern replacement you should reach for first.

Deprecated & Removed: ftp_quit() is an alias of ftp_close(). The alias was deprecated in PHP 7.2 and removed in PHP 8.0. In any project running PHP 8 or later, call ftp_close() instead — it behaves identically. This guide documents ftp_quit() for maintaining legacy code.

What is ftp_quit()?

The ftp_quit() function accepts a single parameter:

  1. ftp_stream — the connection identifier returned by ftp_connect() (or ftp_ssl_connect()).

It returns true on success, or false on failure.

Why close an FTP connection?

PHP automatically closes an FTP connection when the script ends, so why call ftp_quit() explicitly?

  • Free resources early. In a long-running script that opens several connections (for example, syncing to multiple servers), closing each one as soon as you are done prevents you from holding idle sockets open.
  • Send a clean QUIT. A graceful close tells the server you are leaving, which lets it release its own per-session resources immediately instead of waiting for a timeout.
  • Stay within connection limits. Many FTP servers cap simultaneous connections per user. Releasing connections promptly avoids "too many connections" errors.

Syntax of ftp_quit()

The syntax of the ftp_quit() function is as follows:

Syntax of ftp_quit()

bool ftp_quit ( resource $ftp_stream )

The ftp_quit() function takes one required parameter, ftp_stream. The ftp_stream parameter is the connection identifier returned by the ftp_connect() function.

Usage of ftp_quit()

To use the ftp_quit() function, you first establish a connection with ftp_connect() and authenticate with ftp_login(). Once your transfers are done, you close the session. Here's a typical flow for legacy PHP environments:

Usage of ftp_quit()

<?php

// Set up an FTP connection
$conn = ftp_connect('ftp.example.com');

// Login with your FTP credentials
ftp_login($conn, 'username', 'password');

// ... transfer files here (ftp_put, ftp_get, etc.) ...

// Close the FTP connection (use ftp_close() in PHP 8+)
ftp_quit($conn);

In this example, we connect to the FTP server with ftp_connect(), authenticate with ftp_login(), perform any file transfers, and finally close the connection with ftp_quit().

After ftp_quit() runs, the $conn handle is no longer valid — any further FTP call on it (such as ftp_get() or ftp_pwd()) will fail. If you need to talk to the server again, open a fresh connection.

Error handling in ftp_quit()

It's important to handle errors properly when using the ftp_quit() function. If the function returns false, it means that the operation was unsuccessful. Failures are rare and usually indicate that the connection was already closed. Here's an example of how to handle errors:

Error handling in ftp_quit()

<?php

$connection_closed = ftp_quit($conn);

if (!$connection_closed) {
    echo "Failed to close FTP connection.\n";
}

By checking the return value, you can confirm that the connection was closed as expected. In practice a false result is uncommon and usually means the connection had already been dropped.

ftp_quit() vs. ftp_close()

The two functions are identical in behavior — ftp_quit() is simply an older alias. The only difference is availability:

FunctionAvailable inStatus
ftp_quit()PHP 4 – 7.xDeprecated in 7.2, removed in 8.0
ftp_close()PHP 4 onwardRecommended

If you are writing new code, always use ftp_close(). Reserve ftp_quit() for maintaining code that must run on PHP 7.1 or earlier.

Conclusion

The ftp_quit() function closes an FTP connection in legacy PHP environments, freeing resources and ending the session cleanly. With proper usage and error handling it is a reliable part of FTP workflows on older PHP versions. For PHP 8 and later, replace it with ftp_close(). To explore the full FTP toolkit, see the PHP FTP functions overview.

Practice

Practice
What is the main function of the FTP_QUIT method in PHP?
What is the main function of the FTP_QUIT method in PHP?
Was this page helpful?