How can passing the FTP connection as a parameter to a PHP function improve code versatility and organization?
Passing the FTP connection as a parameter to a PHP function improves code versatility and organization by allowing the function to work with different FTP connections without being tied to a specific one. This makes the code more reusable and easier to maintain as the function can be used with various FTP servers or configurations.
function downloadFileFromFTP($ftpConnection, $remoteFile, $localFile) {
// Download file from FTP server using the provided connection
ftp_get($ftpConnection, $localFile, $remoteFile, FTP_BINARY);
}
// Example usage
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');
downloadFileFromFTP($ftp, '/remote/file.txt', '/local/file.txt');
ftp_close($ftp);