Re: Hoping for a working example of SFTP in PHP
| From: | Jose Nobile | Date: | Fri, 29 Jan 2021 18:12:20 +0000 |
| Subject: | Re: Hoping for a working example of SFTP in PHP | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-327425@lists.php.net to get a copy of this message | ||
Hello,
Instead of
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$sftp = ssh2_sftp($connection);
do:
$connection = ssh2_connect('shell.example.com', 22, array('hostkey'=>
'ssh-rsa'));
if (ssh2_auth_pubkey_file($connection, 'username',
'/home/username/.ssh/id_rsa.pub',
'/home/username/.ssh/id_rsa', 'secret')) {
echo "Public Key Authentication Successful\n";
$sftp = ssh2_sftp($connection);
} else {
die('Public Key Authentication Failed');
}
Taken from https://www.php.net/manual/en/function.ssh2-sftp
and
https://www.php.net/manual/en/function.ssh2-auth-pubkey-file.php
Just a recommendation: The PHP manual is about learning to use PHP, not
about learning programming, algorithms, the examples in the manual is just
a plus. Once just learn to program (I recommend learning it in pseudocode
and then in c, and then in c++) and then you can jump to more high-level
language: PHP, Javascript, Python, any language will be easy once you
become for example a Software engineer.
Regards,
José Nobile
On Mon, Jan 25, 2021 at 7:52 AM David Spector <
david025@springtimesoftware.com> wrote:
> 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 (including those in the PHP Manual) 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, the short FTP insecure example in the PHP
> Manual works perfectly for me. There seem to be many subtle undocumented
> issues with getting any of the published example code using private keys
> to work. I'm inexperienced with SFTP and other advanced features of PHP
> and am hoping to find 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 and is said to work well, but it is a
> very large PHP source code library, so I'd rather use the php_ssh2.dll
> or cURL PHP extension, or something similar.
>
> In summary, I'm looking for someone who has successfully used SFTP file
> upload with private key 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
> increasingly important and useful problem.
>
> David Spector
> Springtime Software
>