2017-07-19 194 views
0

我在將文件從我的laravel項目複製到另一臺服務器時遇到問題。這就是我正在做的。SSH2我做錯了什麼?

$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); 

但它給我這個錯誤

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

我真的很新的SSH我該如何解決這個問題?

+0

我使用的是ssh2,這就是我所知道的@ThomasMoors –

+0

它是否正常返回? ssh2.sftp://資源ID#621 因爲我混淆了錯誤的地方 –

回答

1

在使用ssh2.sftp:// fopen包裝器之前,將$ sftp轉換爲int。

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

ssh2_sftp

上面的示例代碼,除非你施放的$ stftp至(INT),或使用INTVAL()

$流=的fopen(「ssh2.sftp失敗://$ sftp/path/to/file「,'r'); //失敗

$ stream = fopen(「ssh2.sftp://」。(int)$ sftp。「/ path/to/file」,'r'); //喜悅

由於copy()將使用fopen封裝來解釋你的ssh2.sftp:// URI,它應該幫助。

+0

嗨,它仍然是相同的 copy():無法打開ssh2.sftp:// 621 /我的文件夾/僅上載/示例 .txt在遠程主機 –

+0

我的不好,這是創建錯誤的文件夾結構。你的回答是正確的! –