What are the best practices for establishing a secure connection between two servers using PHP for file transfer?

Establishing a secure connection between two servers for file transfer involves using encryption and authentication methods to ensure the data is transmitted securely. One common approach is to use Secure Shell (SSH) for secure file transfer. This can be achieved in PHP by using the SSH2 extension to establish a secure connection between the servers.

// Establish a secure connection between two servers using SSH for file transfer
$connection = ssh2_connect('remote_server', 22);
ssh2_auth_password($connection, 'username', 'password');

// Transfer a file from local server to remote server
ssh2_scp_send($connection, '/path/to/local/file.txt', '/path/to/remote/file.txt');