Are there any best practices to follow when setting up FTP connections in PHP?

When setting up FTP connections in PHP, it is important to follow best practices to ensure secure and reliable file transfers. One key practice is to use secure FTP protocols such as FTPS or SFTP to encrypt data during transmission. Additionally, always validate user input and sanitize FTP connection parameters to prevent security vulnerabilities. Lastly, consider implementing error handling to gracefully manage any connection failures or issues.

<?php

// FTP connection settings
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$ftp_port = 21;

// Connect to FTP server using FTPS
$conn_id = ftp_ssl_connect($ftp_server, $ftp_port);

// Login to FTP server
if (ftp_login($conn_id, $ftp_username, $ftp_password)) {
    echo 'Connected to FTP server.';
} else {
    echo 'Failed to connect to FTP server.';
}

// Close FTP connection
ftp_close($conn_id);

?>