What best practices should be followed when using PHP to upload files to a web server via FTP?

When uploading files to a web server via FTP using PHP, it is important to follow best practices to ensure security and reliability. One key practice is to validate and sanitize user input to prevent any malicious code from being uploaded. Additionally, it is recommended to set appropriate file permissions on the server to restrict access to uploaded files. Lastly, consider using a secure connection (FTP over SSL) for transferring files to protect sensitive data.

<?php
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$local_file = "local_file.txt";
$remote_file = "remote_file.txt";

$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

if (ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY)) {
    echo "File uploaded successfully";
} else {
    echo "Error uploading file";
}

ftp_close($conn_id);
?>