How can FTP connection errors be troubleshooted in PHP scripts?

To troubleshoot FTP connection errors in PHP scripts, you can check for specific error messages returned by the FTP functions and handle them accordingly. Additionally, ensure that the FTP server settings, credentials, and firewall configurations are correct.

<?php
$ftp_server = "ftp.example.com";
$ftp_user = "username";
$ftp_pass = "password";

$conn_id = ftp_connect($ftp_server);
if (!$conn_id) {
    die("Failed to connect to FTP server");
}

$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);
if (!$login_result) {
    die("Failed to login to FTP server");
}

// Continue with FTP operations
ftp_close($conn_id);
?>