What are some best practices for connecting to an FTP server using PHP?

When connecting to an FTP server using PHP, it is important to use secure connections (FTP over SSL) to protect sensitive data. Additionally, always remember to handle errors and exceptions properly to ensure smooth operation of the script. Finally, make sure to close the FTP connection after you are done with your operations to free up server resources.

// Connect to FTP server using secure connection
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';

$conn_id = ftp_ssl_connect($ftp_server);

// Login to FTP server
if (ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    echo "Connected to $ftp_server";
} else {
    echo "Failed to connect to $ftp_server";
}

// Close FTP connection
ftp_close($conn_id);