What are the potential security risks of using FTP for file downloads in PHP scripts?

Using FTP for file downloads in PHP scripts can pose security risks such as exposing credentials in plain text, potential for FTP server vulnerabilities, and lack of encryption for data transfer. To mitigate these risks, it is recommended to use secure protocols like SFTP or HTTPS for file downloads instead.

// Example of using SFTP for file download in PHP
$ftp_server = 'sftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';

$connection = ssh2_connect($ftp_server);
ssh2_auth_password($connection, $ftp_username, $ftp_password);

$sftp = ssh2_sftp($connection);
$remote_file = 'example.txt';
$local_file = '/path/to/local/example.txt';

if (ssh2_scp_recv($connection, $remote_file, $local_file)) {
    echo 'File downloaded successfully.';
} else {
    echo 'Failed to download file.';
}