如何在使用sftp上传文件时修复PHP curl错误CURLE_SSH(79)

时间:2012-04-18 16:04:50

标签: php curl ssh sftp libssh2

我正在尝试重现以下命令,通过我的PHP应用程序上的sftp上传文件:

curl -T /var/repo/file -u user:password sftp://server.com/folder/

以上命令工作正常(我不得不在我的ubuntu盒子上重新编译libcurl以使其工作)。但是,当我尝试使用PHP的curl库时,事情并没有那么顺利。

我正在使用的代码如下:

$ch = curl_init();
$localfile = 'file';
$fp = fopen($localfile, 'r');

curl_setopt($ch, CURLOPT_URL, 'sftp://server.com/folder/');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'user:password');
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);

if ($error_no == 0) {
    $error = 'File uploaded succesfully.';
} else {
    $error = 'File upload error.';
}
echo $error.' '.$error_no;

这回复了错误79:CURLE_SSH(79),我不知道如何修复。你遇到过这个问题吗?你怎么修好它的?有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

这意味着libcurl从“SSH层”(libssh2)中获得了某种错误。如果启用VERBOSE,您可能会看到更多详细信息。

你使用的是相当旧的libcurl和libssh2版本,所以只需将它们升级到现代版本就可以解决这个问题,这是不可想象的。