Re: Hoping for a working example of SFTP in PHP
| From: | Stefan A. | Date: | Mon, 01 Feb 2021 18:39:18 +0000 |
| Subject: | Re: Hoping for a working example of SFTP in PHP | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-327431@lists.php.net to get a copy of this message | ||
Hi David,
Are your keys in the correct place? Based on the script you provided, the
keys should be in the same location as the script, for example if the
script is in C:\scripts the keys should also be in C:\scripts. If the keys
are in another location best bet would be to adapt the script and provide
absolute paths. For example if your script is in C:\scripts and your keys
are in C:\keys you should adapt your script to something like
if (ssh2_auth_pubkey_file($connection, $user,
"C:\keys\x.crt",
"C:\keys\x.key", $passphrase)) {
Also make sure the user that executes the scripts has the permissions to
read those files (x.crt and x.key). Also make sure your keys are in the
appropriate format. The documentation page for ssh2_auth_pubkey_file states
that
pubkeyfile
The public key file needs to be in OpenSSH's format. It should look
something like:
ssh-rsa AAAAB3NzaC1yc2EAAA....NX6sqSnHA8= rsa-key-20121110
Here's the link to that page
https://www.php.net/manual/en/function.ssh2-auth-pubkey-file
Good luck!
On Mon, Feb 1, 2021 at 7:25 PM David Spector <
david025@springtimesoftware.com> wrote:
> This is a reply to: 1/29/2021 1:12 PM, Jose Nobile
>
> Can anyone help me?
>
> My version of your code, adapted for Windows and cPanel, is as follows:
>
> $connection = ssh2_connect($hostDomainName, $securePort,
> array('hostkey'=>'ssh-rsa'));
> echo "ssh2_connect: $connection";
> if (ssh2_auth_pubkey_file($connection, $user,
> "x.crt",
> "x.key", $passphrase)) {
> echo "Public Key Authentication Successful\n";
> $sftp = ssh2_sftp($connection);
> } else {
> die('Public Key Authentication Failed');
> }
>
> Running this code produces an error:
>
> "Starting...
> ssh2_connect: Resource id #4
> Warning: ssh2_auth_pubkey_file(): Authentication failed for nsr using
> public key: Unable to open public key file in
> C:\Dropbox\Private\Web\nsr-HMAC\upload\upload.php on line 39
> Public Key Authentication Failed"
>
> File x.crt begins:
>
> -----BEGIN CERTIFICATE-----
>
> File x.key begins:
>
> "-----BEGIN RSA PRIVATE KEY-----
> Proc-Type: 4,ENCRYPTED"
>
> Can anyone help me?
>
> David Spector
> Springtime Software
>