SSH2 what am I doing wrong?

I'm having trouble copying a file from my laravel project to another server. This is what I'm doing.

    $connection = ssh2_connect($this->ftpHostname, $this->ftpPort);

    if (!$connection) {
        throw new \Exception("Couldn't connect to {$this->ftpHostname}:{$this->ftpPort}");
    }

    $loginResult = ssh2_auth_password($connection, 'usrname', 'pswrd');

    if (!$loginResult) {
        throw new \Exception("Username or Password not accepted for {$this->ftpHostname}:{$this->ftpPort}");
    }

    $sftp = ssh2_sftp($connection);
    $fullFilePath = storage_path() .'/'.$this->argument('local-file');
    $remoteFilePath = "ssh2.sftp://{$sftp}/{$this->argument('remote-folder')}/SomeFolder/{$this->argument('remote-filename')}.txt";

    $copyResult = copy($fullFilePath, $remoteFilePath);

But it's giving me this error

[ErrorException]
copy(): Unable to open ssh2.sftp://Resource id #621/My Folders/Upload only/sample.txt on remote host

I'm really new in ssh how do I solve this?

Upvotes: 0

Views: 1954

Answers (1)

Symeon Quimby
Symeon Quimby

Reputation: 869

Cast $sftp to an int before using it in the ssh2.sftp:// fopen wrapper.

$remoteFilePath = "ssh2.sftp://" . (int)$sftp . "/{$this->argument('remote-folder')}/SomeFolder/{$this->argument('remote-filename')}.txt";

From ssh2_sftp

The example code above fails unless you cast the $stftp to (int) or use intval()

$stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r'); // Fails

$stream = fopen("ssh2.sftp://" . (int)$sftp . "/path/to/file", 'r'); // joy

Since copy() will be using the fopen wrappers to interpret your ssh2.sftp:// uri, that should help.

Upvotes: 2

Related Questions