What potential pitfalls should be considered when writing a PHP script to read a file from an FTP server?

One potential pitfall to consider when writing a PHP script to read a file from an FTP server is handling errors that may occur during the file transfer process. It is important to check for errors and handle them gracefully to prevent the script from crashing or leaking sensitive information. Additionally, make sure to properly sanitize user input to prevent security vulnerabilities such as directory traversal attacks.

<?php

// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$ftp_conn = ftp_connect($ftp_server);
$login = ftp_login($ftp_conn, $ftp_user, $ftp_pass);

if (!$ftp_conn || !$login) {
    die('FTP connection failed');
}

// Get file from FTP server
$remote_file = '/path/to/file.txt';
$local_file = 'local_file.txt';

if (ftp_get($ftp_conn, $local_file, $remote_file, FTP_BINARY)) {
    echo "File downloaded successfully";
} else {
    echo "Failed to download file";
}

// Close FTP connection
ftp_close($ftp_conn);

?>