【问题标题】:PHP SSH2 operations failing when attempting to upload file to server尝试将文件上传到服务器时 PHP SSH2 操作失败
【发布时间】:2015-03-08 16:04:16
【问题描述】:

我目前正在努力使用 PHP 的 SSH2 内置库(运行版本 5.5)。如标题所述,我正在尝试将文件上传到 SFTP 服务器,但是我不断收到“流操作失败”错误消息。

在尝试调试代码本身后,连接工作正常,sftp 资源被正确分配了一个 ID,但是当调用 fopen 将文件直接写入远程服务器时,它会失败。

// open Live environment if we are not in dev
$connection = ssh2_connect($this->_settings['source_host'], 22);
$authSuccess = ssh2_auth_password($connection, $this-  >_settings['source_user'], $this->_settings['source_password']);
$sftp = ssh2_sftp($connection);

最后是 fopen() 调用:

if($operation == 'export') {
    $handle = fopen("ssh2.sftp://".$sftp."/remotecopy/IN/".$filename, $mode);
}

我在自己的代码中添加了调试消息,以验证 _settings 数组中的数据是否也被正确使用,但我无法解释流错误。

Message:  fopen(): Unable to open ssh2.sftp://Resource id #173/PATH GOES HERE/filename.xxx on remote host

Message:  fopen(ssh2.sftp://Resource id #173/PATH GOES HERE/filename.xxx): failed to open stream: operation failed

请注意,该文件在远程主机上不存在,但据我所知,PHP fopen() 中的“w”模式应该在文件不存在时创建该文件。

我不能使用其他 PHP 库,因为我们的整个项目都使用内置的 ssh2 库,负责人告诉我不要使用它,因为它在其他任何地方都可以正常工作。

【问题讨论】:

  • fopen 是否可能在您的服务器上被禁用?
  • 也许,我不知道。验证后好像开启了。

标签: php libssh2 ssh2-sftp


【解决方案1】:

如果您使用phpseclib, a pure PHP SFTP implementation,我认为您会更轻松。例如。

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

// puts a three-byte file named filename.remote on the SFTP server
$sftp->put('filename.remote', 'xxx');
// puts an x-byte file named filename.remote on the SFTP server,
// where x is the size of filename.local
$sftp->put('filename.remote', 'filename.local', NET_SFTP_LOCAL_FILE);
?>

phpseclib 的优点之一是它可以进行日志记录,因此如果这不起作用,您可以在包含 Net/SFTP.php 之后执行define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);,然后在它失败后执行echo $sftp-&gt;getLog()。如果它仍然不起作用,这可能会提供一些关于正在发生的事情的见解。

【讨论】:

    【解决方案2】:

    答案很简单,我在远程服务器上的路径格式不正确。验证我的设置后,它工作得很好。

    感谢大家的提示和帮助。

    【讨论】:

      猜你喜欢
      • 2019-11-29
      • 1970-01-01
      • 2016-09-26
      • 2015-11-24
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2011-05-23
      相关资源
      最近更新 更多