'How To Connect SFTP With RSA Key Using PHP Native?

Previously I connected to SFTP using Password & Username and this work:

if (!function_exists("ssh2_connect"))
            die('Function ssh2_connect not found, you cannot use ssh2 here');
        
        if (!$connection = ssh2_connect($host, $port))
            die('Unable to connect');
        
        if (!ssh2_auth_password($connection, $username, $password))
            die('Unable to authenticate.');
        
        if (!$stream = ssh2_sftp($connection))
            die('Unable to create a stream.');
        
        if (!$dir = opendir("ssh2.sftp://{$stream}{$remoteDir}"))
            die('Could not open the directory');
        
        $files = array();
        while (false !== ($file = readdir($dir)))
        {
            $filearr = explode(".", $file);
            $getFileName = $filearr[0];
            
            if ($getFileName == $fileName){
                $files[] = $file;
            }
        }

How connect to SFTP with RSA key using PHP Native (Not Library) ?

php


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source