w3docs logo

Understanding the PHP Function 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 ftp_connect() function as its parameter and returns a boolean value. If the function is successful in closing the connection, it returns true. Otherwise, it returns false.

Syntax of ftp_close()

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

bool ftp_close ( resource $ftp_stream )

The ftp_close() function takes one parameter: ftp_stream. The ftp_stream parameter is the connection identifier returned by the ftp_connect() function.

Usage of ftp_close()

To use the ftp_close() function, you first need to establish a connection to the FTP server using the ftp_connect() function. Here's an example:


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

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

// Do some FTP operations...

// Close the connection

In this example, we establish a connection to the FTP server using the ftp_connect() function. Then we log in using our FTP credentials using the ftp_login() function. Finally, we perform some FTP operations and close the connection using the ftp_close() function.

Error handling in ftp_close()

It's important to handle errors properly when using the ftp_close() function. If the function returns false, it means that the connection couldn't be closed for some reason. Here's an example of how to handle errors:


if (ftp_close($conn) === false) {
    echo "Failed to close the connection.\n";
} else {
    echo "Connection closed successfully.\n";

In this example, we check the return value of the ftp_close() function. If it's false, we display an error message; otherwise, we display a success message.


In conclusion, the ftp_close() function is a useful PHP built-in function that allows you to close an FTP connection. By following the guidelines and best practices outlined in this article, you can use the ftp_close() function in your PHP projects with confidence. We hope this article has been helpful to you and provided you with the necessary information about the function. If you have any further questions or need additional assistance, please don't hesitate to reach out to us.

Do you find this helpful?