What is the purpose of the PHP function ftp_fget in the given code snippet?
The purpose of the PHP function ftp_fget in the given code snippet is to download a file from a remote FTP server to a local file on the server using FTP. It allows for the retrieval of a file from the FTP server and saves it locally. Here is a complete PHP code snippet that demonstrates the use of ftp_fget to download a file from a remote FTP server:
<?php
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$local_file = "localfile.txt";
$remote_file = "remotefile.txt";
// Connect to FTP server
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
if ($conn_id && $login_result) {
// Download file from FTP server
$local_handle = fopen($local_file, 'w');
if (ftp_fget($conn_id, $local_handle, $remote_file, FTP_BINARY)) {
echo "File downloaded successfully.";
} else {
echo "Failed to download file.";
}
// Close file handles and FTP connection
fclose($local_handle);
ftp_close($conn_id);
} else {
echo "Failed to connect to FTP server.";
}
?>