php - SSH2 我做错了什么?

标签 php linux laravel sftp ssh2-sftp

我无法将文件从我的 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 新手,我该如何解决这个问题?

最佳答案

在 ssh2.sftp://fopen 包装器中使用它之前,先将 $sftp 转换为 int。

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

来自 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

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

关于php - SSH2 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45192370/

相关文章:

mysql - 错误 "Failed to start mariadb.service: Unit mariadb.service not found"

laravel 5.2 :Adding external php files to laravel

php - 使用 codeigniter 在 sql 中仅选择匹配值

php - Laravel SQLSTATE[HY000] [2002] 当命令在 "Controller"时连接被拒绝,但 Tinker 中的单词正常

linux - 安装 debian 后 windows 7 无法启动

linux - 删除 ls -l 输出中的多个空格

laravel - 使用服务提供商的中间件获取覆盖的配置

php - 使用 axios 以正确的时间从数据库中获取条目

php - 我的下拉列表仅显示数据库中数据的第一个字母表

php - 使用 Laravel 5 执行 SQL 函数