作者:大布丁 | 来源:互联网 | 2023-06-29 14:32
我遵循了this Stack Overflow question thread的建议,但我一直在努力.
我收到以下错误消息:不支持的协议:sftp
这是我的代码:
$ch = curl_init();
if(!$ch)
{
$error = curl_error($ch);
die("cURL session could not be initiated. ERROR: $error."");
}
$fp = fopen($docname, 'r');
if(!$fp)
{
$error = curl_error($ch);
die("$docname could not be read.");
}
curl_setopt($ch, CURLOPT_URL, "sftp://$user_name:$user_pass@$server:22/$docname");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($docname));
//this is where I get the failure
$exec = curl_exec ($ch);
if(!$exec)
{
$error = curl_error($ch);
die("File $docname could not be uploaded. ERROR: $error.");
}
curl_close ($ch);
我使用curl_version()函数来查看我的curl信息,发现sftp似乎不在支持的协议数组中:
[version_number] => 462597
[age] => 2
[features] => 1597
[ssl_version_number] => 0
[version] => 7.15.5
[host] => x86_64-redhat-linux-gnu
[ssl_version] => OpenSSL/0.9.8b
[libz_version] => 1.2.3
[protocols] => Array
(
[0] => tftp
[1] => ftp
[2] => telnet
[3] => dict
[4] => ldap
[5] => http
[6] => file
[7] => https
[8] => ftps
)
这是我的cURL版本过时的问题,还是根本不支持SFTP协议?
任何意见是极大的赞赏.
解决方法:
也许尝试使用phpseclib, a pure PHP SFTP implementation.例如
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');
?>