Hoping to get a working example of SFTP in PHP

David Spector david025 at springtimesoftware.com
Mon Jan 25 02:11:11 UTC 2021

This question may be considered off-topic, since is not directly about 
using the OpenSSL library. Let me know if you want me to delete this 

I have a question about uploading a file (text.txt) securely in PHP 
using the SFTP protocol and a public/private key pair. I have posted 
this question is several fora, but no one seems to know for sure how to 
do it. Although I've received much advice, none of the actual published 
examples are known to work, and do not work for me. I know my parameters 
are okay, since they work in CoreFTP and PuTTY, two manual applications 
that implement SFTP with private keys. Also, a short FTP insecure 
example works perfectly for me. There seem to be many subtle 
undocumented issues with getting any of the published example code to 
work. I'm inexperienced with SFTP in PHP and am hoping to locate a 
working example.

So far I have tried a number of examples using the ssh2 PHP extension 
and the cURL PHP extension. They fail, and the error messages, if any, 
are uninformative.

A little background:

I do website development work for two small companies on a Windows 
computer (Apache/PHP 7), uploading to a remote production server 
(VPS/cPanel/Centos). My local software versions are: Windows 10 
Hone/2004, Bitnami WAMP/7.4.13-0, Apache/2.4.46 (Win64), OpenSSL/1.1.1h, 
PHP/7.4.13, libssh2/1.9.0.

Where I am now is that I used cPanel on the remote server to install an 
SSH/SFTP public key for a particular account. I then copied the 
generated encrypted x.key and x.ppk private key files to my local 
computer. Now I am ready to upload files securely, if only PHP made the 
task easy. Unfortunately, it does not.

I know that phpseclib is available, but it is a very large PHP source 
code library, so I'd rather use the php_ssh2.dll or cURL PHP extension, 
the OpenSSL PHP extension (if it can do SFTP), or something similar.

In summary, I'm looking for someone who has got SFTP file upload with 
private key working in PHP to share their code with me. I pledge to test 
the code and post it further to help others with this difficult but 
widely useful problem.

More information about the openssl-users mailing list