How can PHP developers ensure that their code is secure when interacting with external resources like FTP servers?
PHP developers can ensure that their code is secure when interacting with external resources like FTP servers by validating user input, sanitizing data before sending it to the server, and using secure connection methods like SFTP instead of FTP. They should also handle errors properly and avoid storing sensitive information like passwords in plain text.
// Example of secure FTP connection using SFTP
$ftp_server = 'sftp://example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$connection = ssh2_connect($ftp_server);
if (ssh2_auth_password($connection, $ftp_username, $ftp_password)) {
echo "Connected to SFTP server successfully!";
} else {
echo "Failed to connect to SFTP server.";
}