How can the use of the copy() function in PHP be helpful in downloading and transferring files via FTP?

The copy() function in PHP can be helpful in downloading and transferring files via FTP by allowing you to easily copy a file from one location to another. This function can be used to transfer files from a remote FTP server to a local directory or vice versa. By specifying the file paths and using the copy() function within an FTP connection, you can efficiently transfer files between locations.

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

// Download file from FTP server
$remote_file = '/path/to/remote/file.txt';
$local_file = '/path/to/local/file.txt';
if (ftp_get($conn_id, $local_file, $remote_file, FTP_BINARY)) {
    echo "File downloaded successfully\n";
} else {
    echo "Failed to download file\n";
}

// Close FTP connection
ftp_close($conn_id);