How to Transfer files from Acquia to an External Server

Zyxware default image3

One of our project requirement was to transfer files from our server to an external storage space. This could be basically done by connecting to the server using SSH or FTP and then transferring files. We initially decided to use the libssh2-php library for creating SSH tunneling with the server and transfer file. Our server was hosted in Acquia server and libssh2-php was not available in Acquia stack. Read on to know how we tackled the problem of file transfer to an external storage space in Acquia environment.

Since libssh2-php could not be installed in Acquia server, we looked for alternatives for transferring files to an external source. phpseclib library suited our requirement as it does not require any extension and provides SSH as well as SFTP connection. Have a look at the following code to establish connection and transfer files.



function mymodule_amazon_connect() {
  require_once 'Net/SFTP.php';
  $file_contents = file_get_contents('myfile.txt', TRUE);
  // Setup connection.
  $sftp = new Net_SFTP('www.mydomain.com');
  if (!$sftp->login('username', 'password')) {
    exit('Login failed!');
  }
  $sftp->put('myfile.txt', $file_contents);
}

Hope this helps. Also, if you need any further assistance please feel free to get in touch with us