How can the ftp_get() function be used to improve file transfer reliability compared to the copy() function?
When transferring files using the copy() function in PHP, there is a risk of the transfer being interrupted, leading to an incomplete or corrupted file at the destination. To improve file transfer reliability, the ftp_get() function can be used instead. This function allows files to be transferred over FTP, which is more robust and can handle interruptions better than a simple copy operation.
// Using ftp_get() function for improved file transfer reliability
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$local_file = 'local_file.txt';
$remote_file = 'remote_file.txt';
// Connect to FTP server
$ftp_conn = ftp_connect($ftp_server);
ftp_login($ftp_conn, $ftp_username, $ftp_password);
// Get file from FTP server and save locally
if (ftp_get($ftp_conn, $local_file, $remote_file, FTP_BINARY)) {
echo "File transfer successful";
} else {
echo "File transfer failed";
}
// Close FTP connection
ftp_close($ftp_conn);