5

I followed the advice from this Stack Overflow question thread, but I keep hitting a snag.

I am receiving the following error message: Unsupported protocol: sftp

Here is my code:

$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);

I used the curl_version() function to see my curl information, and found that sftp doesn't seem to be in the array of supported protocols:

[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
        )

Is this a matter of my version of cURL being outdated, or is the SFTP protocol simply not supported at all?

Any advice is greatly appreciated.

Community
  • 1
  • 1
Bad Programmer
  • 3,642
  • 13
  • 46
  • 53
  • 3
    It looks like `libcurl` on your system does not have sftp support (not all distributions provide sftp support in curl out of the box). See [here](http://iamnearlythere.com/add-sftp-capabilities-to-curl/) for example how to enable sftp support in curl on ubuntu. Maybe this question would be more suitable on [serverfault.com](http://serverfault.com). – poncha Jun 26 '12 at 22:30
  • SFTP Support, Check SSL / TLS support first. It just could be that you have no "S" support at all. That is totally unrelated to the concrete PHP code, it's just whether or not you need/have "S" (Secure) support. If not (as it looks like) contact your sysadmin and ask to install it. – hakre Jun 27 '12 at 01:11

2 Answers2

3

Maybe try using phpseclib, a pure PHP SFTP implementation. eg.

<?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');
?>
neubert
  • 15,947
  • 24
  • 120
  • 212
1

As of this writing, there is no longer a need to depend on and use curl's sftp implementation if you have access to php. Goto php.net and look at ssh2 support. There you will find an how to deploy sftp programmatically.

An alternative, still in php, is to use the Net_SFTP class/package to found at phpseclib.sourceforge.net.

The documentation is at http://phpseclib.sourceforge.net/documentation/net.html#net_sftp.

Expect?

In the unlikely event that you don't have access to php or you don't want to use php, I suggest that you can still stop worrying about curl's sftp implementation by using expect. If you do not already have expect on your system, goto www.nist.gov/el/msid/expect.cfm to get it. Once you get it and install it, then to scripting sftp will look something similar to

#!/usr/bin/expect
spawn /usr/bin/sftp <user@hostname>
expect "password:"
send "<mypassword>\r"
expect "sftp> "
send "get <remotefile> \r"
expect "sftp> "
send "bye \r"
exit 0

where you will replace with your own values.

The idea is to stop wasting time on trying to use sftp with curl. You can use php's implementation or you can script sftp using expect. Hope this helps and saves someone from wasting time.

alexis
  • 48,685
  • 16
  • 101
  • 161
Adams
  • 11
  • 1