get() public method

Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the operation. $offset and $length can be used to download files in chunks.
public get ( string $remote_file, string $local_file = false, integer $offset, integer $length ) : mixed
$remote_file string
$local_file string
$offset integer
$length integer
return mixed
Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function download($local, $remote)
 {
     $this->checkConnection();
     if (!$this->sftp->get($remote, $local)) {
         throw new \RuntimeException(implode($this->sftp->getSFTPErrors(), "\n"));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getFile($remoteFilename, $localFilename)
 {
     if ($this->isConnected()) {
         return $this->connection->get($remoteFilename, $localFilename);
     }
     return false;
 }
Esempio n. 3
0
 public function downloadFile($File)
 {
     $this->persistConnection();
     if (!$this->Connection->get($File, $File)) {
         throw new ComponentException(__METHOD__ . ': Failed');
     }
     return $this;
 }
Esempio n. 4
0
 public function get($src, $dest = null)
 {
     if (false === $this->isConnected()) {
         $this->connectAndLogin();
     }
     $realDest = $dest;
     if (null == $dest) {
         $realDest = tempnam(sys_get_temp_dir(), basename($src));
     }
     $this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_GET, new TransporterEvent($this, array('src' => $src, 'dest' => $dest)));
     $succes = $this->sftp->get($src, $realDest);
     if (null == $dest) {
         $content = file_get_contents($realDest);
         unlink($realDest);
         return $content;
     }
 }
Esempio n. 5
0
 /**
  * Download file from remote account.
  *
  * @param string $absolutePathRemoteFile The absolute path to remote file (new)
  * @param string $absolutePathLocalFile  The absolute path to local file
  *
  * @return SftpInterface The current instance
  *
  * @api
  */
 public function downloadFile(string $absolutePathRemoteFile, string $absolutePathLocalFile) : SftpInterface
 {
     $this->netSftp->get($absolutePathRemoteFile, $absolutePathLocalFile);
     return $this;
 }
Esempio n. 6
0
 /**
  * Executes the PullDbViaSsh Task.
  *
  * @return Robo\Result
  */
 public function run()
 {
     // Login to the remote server
     $this->printTaskInfo('Logging into remote server - <info>ssh://' . $this->sshUser . '@' . $this->sshHost . '/</info>');
     $ssh = new SFTP($this->sshHost);
     // Do we use password or a key
     if (file_exists($this->sshKey) && empty($this->sshPass)) {
         $key = new RSA();
         $key->loadKey(file_get_contents($this->sshKey));
         if (!$ssh->login($this->sshUser, $key)) {
             throw new RuntimeException('Failed to login via SSH using Key Based Auth.');
         }
     } else {
         if (!$ssh->login($this->sshUser, $this->sshPass)) {
             throw new RuntimeException('Failed to login via SSH using Password Based Auth.');
         }
     }
     // Create our dump filename
     $dump_name = $this->remoteDbName . '_' . time();
     // Create our dump on the remote server
     $cmd = 'mysqldump ' . '-h' . $this->remoteDbHost . ' -u' . $this->remoteDbUser . ' ' . (empty($this->remoteDbPass) ? '' : '-p' . $this->remoteDbPass) . ' ' . $this->remoteDbName . ' > /tmp/' . $dump_name . '.sql';
     $this->printTaskInfo('Dumping db on remote server - <info>' . $cmd . '</info>');
     $results = $ssh->exec($cmd);
     if ($ssh->getExitStatus() > 0) {
         throw new RuntimeException('Failed to create dump on remote server. ' . $results);
     }
     // Compressing dump
     $cmd = 'gzip /tmp/' . $dump_name . '.sql';
     $this->printTaskInfo('Compressing dump on remote server - <info>' . $cmd . '</info>');
     $results = $ssh->exec($cmd);
     if ($ssh->getExitStatus() > 0) {
         throw new RuntimeException('Failed to compress dump on remote server. ' . $results);
     }
     // Copy it down locally
     $this->printTaskInfo('Transfering dump to local.');
     $temp_dump_name = tempnam(sys_get_temp_dir(), 'dump');
     $temp_dump = $temp_dump_name . '.sql.gz';
     if (!$ssh->get('/tmp/' . $dump_name . '.sql.gz', $temp_dump)) {
         throw new RuntimeException('Failed to download dump.');
     }
     // Remove the dump from the remote server
     $this->printTaskInfo('Removing dump from remote server - <info>rm /tmp/' . $dump_name . '.sql.gz</info>');
     if (!$ssh->delete('/tmp/' . $dump_name . '.sql.gz')) {
         throw new RuntimeException('Failed to delete dump on remote server.');
     }
     // Import the dump locally
     if (!$this->taskImportSqlDump($temp_dump)->host($this->localDbHost)->user($this->localDbUser)->pass($this->localDbPass)->name($this->localDbName)->run()->wasSuccessful()) {
         throw new RuntimeException('Failed to import dump on local server.');
     }
     $this->printTaskInfo('Deleting dump locally.');
     unlink($temp_dump);
     unlink($temp_dump_name);
     // If we get to here assume everything worked
     return Result::success($this);
 }
Esempio n. 7
0
 /**
  * @param string $identifier
  * @return string
  */
 public function readFile($identifier)
 {
     return $this->sftp->get($identifier);
 }