You are here

public function BackupMigrateDestinationSFTP::load_file in Backup and Migrate SFTP 6

Same name and namespace in other branches
  1. 7 destinations.sftp.inc \BackupMigrateDestinationSFTP::load_file()

Download a remote file to the local server.

File

./destinations.sftp.inc, line 65
Functions to handle the SFTP backup destination.

Class

BackupMigrateDestinationSFTP
A destination for sending database backups to an SFTP server.

Code

public function load_file($file_id) {
  backup_migrate_include('files');
  $file = new backup_file(array(
    'filename' => $file_id,
  ));
  if (!$this
    ->sftpConnect()) {
    return FALSE;
  }
  $sftp = $this->sftp;
  $path = $this->dest_url['path'];
  $stream = @fopen("ssh2.sftp://{$sftp}/{$path}/{$file_id}", 'r');
  if (!$stream) {
    throw new Exception("Could not open file: {$file_id}");
    return FALSE;
  }
  $data = fread($stream, filesize("ssh2.sftp://{$sftp}/{$path}/{$file_id}"));
  if (!$data) {
    throw new Exception("Could not transfer file.");
    return FALSE;
  }
  file_put_contents($file
    ->filepath(), $data);
  @fclose($stream);
  return $file;
}