What are potential pitfalls when using FTP to transfer files in PHP?

Potential pitfalls when using FTP to transfer files in PHP include security vulnerabilities, such as exposing sensitive information like login credentials, and potential data breaches if the FTP server is not properly secured. To mitigate these risks, it is recommended to use secure FTP protocols like SFTP or FTPS, and always sanitize user input to prevent injection attacks.

// Example of using SFTP to transfer files securely
$ftp_server = 'sftp://example.com';
$ftp_user = 'username';
$ftp_pass = 'password';

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

if ($conn_id && $login_result) {
    // File transfer logic here
    ftp_close($conn_id);
} else {
    echo "Failed to connect to SFTP server";
}