尝试将文件上载到服务器时PHP SSH2操作失败

尝试将文件上载到服务器时PHP SSH2操作失败,php,libssh2,ssh2-sftp,Php,Libssh2,Ssh2 Sftp,我目前正在努力使用运行5.5版的PHP的SSH2内置库。我试图上传一个文件到一个SFTP服务器作为标题状态,但我不断得到一个流操作失败的错误消息 在尝试调试代码之后,连接工作正常,sftp资源被正确分配了一个ID,但是当调用fopen将文件直接写入远程服务器时,它会失败 // open Live environment if we are not in dev $connection = ssh2_connect($this->_settings['source_host'], 22);

我目前正在努力使用运行5.5版的PHP的SSH2内置库。我试图上传一个文件到一个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库,负责人告诉我不要使用它,因为它在其他任何地方都可以正常工作

我想如果你用的话,你会过得更轻松些。例如


phpseclib的一个优点是它的日志记录功能,因此如果这不起作用,您可以定义“NET_SSH2_日志记录”,NET_SSH2_LOG_COMPLEX;在包含Net/SFTP.php之后,然后在失败点之后执行echo$SFTP->getLog。如果它仍然不工作,这可能会提供一些关于发生了什么的见解。

答案很简单,我在远程服务器上设置了错误的路径格式。在验证我的设置后,它工作正常


谢谢大家的提示和帮助

是否可能您的服务器上禁用了fopen?也许,我不知道。似乎是在验证后启用的。
<?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);
?>