0

我无法将文件从我的 laravel 项目复制到另一台服务器。这就是我正在做的事情。

    $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 的新手,我该如何解决这个问题?

4

1 回答 1

2

在 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(),否则上面的示例代码将失败

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

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

由于copy()将使用 fopen 包装器来解释您的 ssh2.sftp:// uri,这应该会有所帮助。

于 2017-07-19T14:12:14.360 回答