0

I am not able to connect to Sftp in magento2.2.5

Here is my code:

public function execute()
    {
        $connection = $this->sftp->open(
                    array(
                        'host' => 'myhostname',
                        'username' => 'myusername',
                        'password' => 'mypassword',
                        'port' =>22,
                        'passive' => true
                    )
                );
        if($connection){
        echo "true";

        }else{
            echo "false";
        }
                //print_r($connection);
        die;

But it is returning false.

I don't know what is the problem.

Any help would be appreciated.

4
  • remove if and else and check by var_dump($this->sftp->ls()); Commented May 24, 2019 at 11:23
  • The connection is being listed.. But the connection is failed.
    – Amy
    Commented May 24, 2019 at 11:32
  • Are you working on cloud magento ? Commented May 24, 2019 at 11:52
  • Yes, I am working on cloud..
    – Amy
    Commented May 24, 2019 at 12:07

2 Answers 2

1

In Magento, $this->sftp->open(); returns null. you can php function is_null() function in the condition.

public function execute()
    {
        $connection = $this->sftp->open(
                    array(
                        'host' => 'myhostname',
                        'username' => 'myusername',
                        'password' => 'mypassword',
                        'port' =>22,
                        'passive' => true
                    )
                );
        if(is_null($connection)){
        echo "true";
         //Here your code.....

        }else{
            echo "false";
        }
0

Kindly install PHP ssh2 extension and then use the following code.

If you get not connected double check your IP is whitelisted or not

$strServer = '*******';
$strServerPort = '*******';
$strServerUsername = '*******';
$strServerPassword = '*******';

//connect to server
$resConnection = ssh2_connect($strServer, $strServerPort);

if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword))
{
     //Initialize SFTP subsystem            
     echo "connected";
}
else
{
      echo "not connected";
} 

It works for me.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.