无法连接到您提供的 public 密钥。 Php |代码点火器

Unable to connect with the public key you've provided. Php | Codeigniter

无法连接到您提供的 public 密钥。Unable to connect with the public key you've provided。

我正在使用 codeigniter 3.1.x。钥匙有效。我已经使用 filezilla 客户端测试了密钥。一切正常,但在 php codeigniter 中出现以下错误。 那里的任何伙伴都可以帮助我解决这个错误。或者可以在适当的指导下建议任何其他 php 库。 提前致谢

 function sftp()
    {
     $this->load->library('SFTP');
     $private_key=APPPATH.'sftp_keys/new_private.ppk';
     $public_key=APPPATH.'sftp_keys/new_public';
     $config=array();
     $config['hostname']='host';
     $config['username']='username';
     $config['port']='22';
     $config['public_key_url']=$public_key;
     $config['private_key_url']=$private_key;
     $config['method']='auth';
     $config['debug']=TRUE;
     $config['login_via_key']=TRUE;
     $this->sftp->initialize($config);
     
     $connect=$this->sftp->connect($config);
     var_dump($connect);
     $this->ftp->close();
    }

var_dump($config)

array(8) { 
  ["hostname"]=> string(13) "" 
  ["username"]=> string(7) "" 
  ["port"]=> string(2) "22" 
  ["public_key_url"]=> string(62) "/home/sgfasaco/public_html/qa/application/sftp_keys/new_public" 
  ["private_key_url"]=> string(71) "/home/sgfasaco/public_html/qa/application/sftp_keys/id_rsa(private).ppk" 
  ["method"]=> string(3) "key" 
  ["debug"]=> bool(true) 
  ["login_via_key"]=> bool(true)
} 

是的,我通过尝试不同的库找到了解决方案。我发现最好的一个工作得很好 PHP(PHP 库)的 Flysystem 文件系统抽象。

https://flysystem.thephpleague.com/v2/docs/

使用命令安装库。

composer require league/flysystem-sftp:^2.0

使用以下代码通过文件系统连接 SFTP。

<?php
require_once('vendor/autoload.php');
use League\Flysystem\Filesystem;
use League\Flysystem\PhpseclibV2\SftpConnectionProvider;
use League\Flysystem\PhpseclibV2\SftpAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use League\Flysystem\DirectoryAttributes;
use League\Flysystem\FileAttributes;
$private_key='sftp_keys/new_private.ppk';

$filesystem = new Filesystem(new SftpAdapter(
    new SftpConnectionProvider(
        'host', // host (required)
        'username', // username (required)
        null, // password (optional, default: null) set to null if privateKey is used
        $private_key, // private key (optional, default: null) can be used instead of password, set to null if password is set
        null, // passphrase (optional, default: null), set to null if privateKey is not used or has no passphrase
        22, // port (optional, default: 22)
        false, // use agent (optional, default: false)
        30, // timeout (optional, default: 10)
        10, // max tries (optional, default: 4)
        null, // host fingerprint (optional, default: null),
        null // connectivity checker (must be an implementation of 'League\Flysystem\PhpseclibV2\ConnectivityChecker' to check if a connection can be established (optional, omit if you don't need some special handling for setting reliable connections)
    ),
    '/', // root path (required)
    PortableVisibilityConverter::fromArray([
        'file' => [
            'public' => 0640,
            'private' => 0604,
        ],
        'dir' => [
            'public' => 0740,
            'private' => 7604,
        ],
    ])
));
$allFiles = $filesystem->listContents('Outbound')->toArray();
$response = $filesystem->write('Outbound/info.txt', 'Hello How are you',array());
if($response){
    echo "Success";
}else echo "Error";
print_r($allFiles );
?>

Composer.json 看起来像

{
    "name": "league/flysystem-sftp",
    "description": "Flysystem adapter for SFTP",
    "license": "MIT",
    "authors": [
        {
            "name": "Frank de Jonge",
            "email": "info@frenky.net"
        }
    ],
    "require": {
        "php": ">=5.6.0",
        "league/flysystem-sftp": "^2.1",
        "phpseclib/phpseclib": "~2.0"
    },
    "require-dev": {
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "^5.7.25"
    },
    "autoload": {
        "psr-4": {
            "League\Flysystem\Sftp\": "src/"
        }
    }
}