Hi all,

I'm having a nightmare trying to sftp using PHP. First of all my research told me that i needed to install the SSH2 library for PHP, for various reasons it has not been possible to install this, i understand there is some kind of conflict with Zend. I'm by no means an expert on php installs but my server admin has told me its just not going to be possible. I then moved on to trying to sFTP using cURL, according to the cURL documentation this is possible in 7.16.1+ but i could find absolutely no documentation how this should be done in php, all i could find was info on FTP with SSL which isnt what i need. I then looked into cURL through the command line, i tried it only to find out that the cURL version on the server was not new enough, the php version of cURL was but not the version on the server (CentOS 4.6). Although the admin wasnt too keen i managed to get him to update cURL to 7.18. I then tested this in the command line and was able to get and put files to the sFTP site - Great!. All i needed to do now was send this command through PHP, presuming this was just going to work i tested this out and nothing happened, checking my error log i saw this:

curl: (1) Unsupported protocol: sftp

Running the exact same command direct in the command line works but when i try to call it through either system() or exec() it errors, what is going on?

Any advice would be greatly appreciated.

thanks a lot.

    from php.net:

    Apparently it is necessary to supply the port as well in the path. Could just be the few systems I was testing against, but that would be three separate networks using three separate platforms (Windows Server, Linux, Unix). At any rate, the following works like a charm, and was very simple to install the libssh2 library and PECL SSH2 module required to use this system. Gotta love PHP.
    ssh2.sftp://$sftp/example.com:22/path/to/file
    ssh2.sftp://user:pass@$sftp:22/example.com/path/to/file

    Hope this saves anyone some headache and time.

    http://www.php.net/function.fopen

      hi, i wasnt able to install SSH2 as i mentioned. the problem is that run in the shell this command works:

      curl -u user:pass -T /home/ftpuser/public_html/test sftp://127.0.0.1/Usr/remoteuser/

      but run in php like this:

      exec("curl -u user:pass -T /home/ftpuser/public_html/test sftp://127.0.0.1/Usr/remoteuser/");
      

      it doesnt work with this:

      curl: (1) Unsupported protocol: sftp

      appearing in the logs.

        adding a --trace gives this info:

        == Info: gethostbyname_r(2) failed for user:pass
        == Info: Couldn't resolve host 'user:pass'
        == Info: Closing connection #0
        
        curl: (6) Couldn't resolve host 'user:pass'
        == Info: Unsupported protocol: sftp
        
        curl: (1) Unsupported protocol: sftp
        

        as i said, this command works when run direct in the shell

          Can you switch around the parameter order so that the sftp:// comes first?

            madwormer2;10883428 wrote:

            Can you switch around the parameter order so that the sftp:// comes first?

            same result if i do that.

              It could be a permissions thing, if it works with the logged in user, and not with whatever PHP is (usually apache, or www etc).

              Out of my depth. Good luck 🙂

                yeah, that sounds like it could be the case maybe, will have to investigate

                  when you do phpinfo() in the first box, what protocols are supported?

                    sorry, what do you mean by first box ?

                      hi all,

                      managed to solve this by placing the full path to curl in the exec call as a "which curl" showed that I was calling a different version of curl from the command line to the one being called by php.

                      so now looks like:

                      exec("/usr/local/bin/curl -u user:pass -T /home/ftpuser/public_html/test sftp://127.0.0.1/Usr/remoteuser/"); 
                        Write a Reply...