why can't I connect sftp with private key, always get message (SSH public key authentication failed: Invalid Username/PublicKey combination) for private key this tried with filezilla and works fine. here i have tried using cURL & ssh2 but all are invalid. can anyone give a solution?
$local_filename = $this->stock;
$fp = fopen($local_filename, 'r');
$sftp_server = $this->ipServer . $this->pathServer . '/STOCK.zip';
$curl = curl_init();
curl_setopt($curl, CURLOPT_UPLOAD, TRUE);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_NOPROGRESS, FALSE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
$trace = tempnam(dirname($local_filename), 'temp_curl_');
$fptrace = fopen($trace, 'w');
curl_setopt($curl, CURLOPT_FILE, $fptrace);
curl_setopt($curl, CURLOPT_STDERR, $fptrace);
curl_setopt($curl, CURLOPT_URL, 'sftp://@' . $sftp_server);
curl_setopt($curl, CURLOPT_PORT, $this->portServer);
curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':');
curl_setopt($curl, CURLOPT_SSH_AUTH_TYPES, CURLSSH_AUTH_PUBLICKEY);
curl_setopt($curl, CURLOPT_SSH_PUBLIC_KEYFILE, $this->publicKey);
curl_setopt($curl, CURLOPT_SSH_PRIVATE_KEYFILE, $this->privateKey);
curl_setopt($curl, CURLOPT_KEYPASSWD, '');
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($local_filename));
curl_setopt($curl, CURLOPT_INFILE, $fp);
$start_error_no = curl_errno($curl);
$valid_operation = curl_exec($curl);
$final_error_no = curl_errno($curl);
curl_close($curl);
fclose($fp);
fclose($fptrace);
echo '<pre>trace:', file_get_contents($trace), '<hr>';
var_dump($valid_operation);
unlink($trace);
die();```
and
$session = @ssh2_connect($this->ipServer, 22, ['hostkey' => 'ssh-rsa']);
if (@ssh2_auth_pubkey_file($session, $this->username, $this->publicKey, $this->privateKey, 'password')) {
echo "Public Key Authentication Successful\n";
} else {
echo ('Public Key Authentication Failed');
}
die();
can login and can upload files.