What considerations should be made when running PHP scripts that involve FTP connections on a web server?

When running PHP scripts that involve FTP connections on a web server, it is important to ensure that the FTP credentials are securely stored and not hardcoded in the script. It is also recommended to use secure FTP protocols such as FTPS or SFTP to encrypt the data transmission. Additionally, error handling should be implemented to gracefully handle any connection failures or issues that may arise during the FTP operations.

<?php
// FTP credentials
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';

// Connect to FTP server
$conn_id = ftp_connect($ftp_server);

if (!$conn_id) {
    die('Failed to connect to FTP server');
}

// Login to FTP server
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

if (!$login_result) {
    die('Failed to login to FTP server');
}

// Perform FTP operations here

// Close FTP connection
ftp_close($conn_id);
?>