supplied resource is not a valid SSH2 SFTP resource

拜拜、爱过 提交于 2019-12-11 06:24:30

问题


<?php
$ssh = ssh2_connect('domain.tld');
ssh2_auth_password($ssh, 'username', 'password');

$start = microtime(true);
$sftp = fopen('ssh2.sftp://'.$ssh.'/home/username/1mb', 'w');

fwrite($sftp, str_repeat('a', 1024 * 1024));
$elapsed = microtime(true) - $start;

echo "took $elapsed seconds";

That code snippet gives me a

PHP Warning: fopen(): supplied resource is not a valid SSH2 SFTP resource in ...

error. Why?

$sftp = fopen('ssh2.sftp://username:password@domain.tld:22/home/user/1mb', 'w');

That one works just fine but it wouldn't work if you were doing public key authentication, for example.


回答1:


As hakre mentioned in comment you should use

$ssh = ssh2_connect('domain.tld');
$resSftp= ssh2_sftp($ssh);
$sftp = fopen('ssh2.sftp://'.$resSftp.'/home/username/1mb', 'w');


来源:https://stackoverflow.com/questions/14309017/supplied-resource-is-not-a-valid-ssh2-sftp-resource

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!