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

When connecting to an FTP server using PHP, it is important to ensure that the connection is secure to protect sensitive data. One way to achieve this is by using FTPS (FTP over SSL/TLS) to encrypt the connection. This can be done by setting the appropriate options when creating an FTP connection in PHP.

// Set up FTP connection with SSL/TLS encryption
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';

$ftp_connection = ftp_ssl_connect($ftp_server);
$login_result = ftp_login($ftp_connection, $ftp_user, $ftp_pass);

if (!$ftp_connection || !$login_result) {
    die('FTP connection failed');
}

// Perform FTP operations here

// Close FTP connection
ftp_close($ftp_connection);