2

I want to connect the SFTP in Magento 2

here is my code.

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$sftp = $objectManager->create('Magento\Framework\Filesystem\Io\Sftp');
$open =  $sftp->open(array('host' => 'hostname','username' => 'username',
'password' => 'password',));

 if ($open)
 {
 echo "true";
 }  
 else
 {
 echo "false";
 }

but does not work that code.

1

I've tested your code, but it works fine, except for the if statement. That if statement always returns false, even if the connection is open. So i've added a var_dump at the end. It lists all existing files in the root of the SFTP server. With that var_dump you can verify the SFTP connection.

<?php

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$sftp = $objectManager->create('Magento\Framework\Filesystem\Io\Sftp');
$open =  $sftp->open(
    array(
        'host' => 'hostname',
        'username' => 'username',
        'password' => 'password',
    )
);

var_dump($sftp->ls());

Hope this helps

| improve this answer | |
  • Yes,my code is workin fine, I think i used wrong password of the sftp and thank for comments. – Rasik Miyani Apr 9 '19 at 15:53
1

I have tested your code with some modification and I am able to connect in your code port no. is missing.

  <?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$sftp = $objectManager->create('Magento\Framework\Filesystem\Io\Sftp');
$open =  $sftp->open(
                    array(
                        'host' => 'myhostname',
                        'username' => 'myusername',
                        'password' => 'mypassword',
                        'port' =>22,
                        'passive' => true
                    )
                );
        if(is_null($connection)){
        echo "true";
        //Here is code ....
        }else{
            echo "false";
        }
| improve this answer | |
0

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

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

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

if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword))
{
     //Initialize SFTP subsystem            
     echo "connected";
} 
| improve this answer | |
0
        $connection = new \Magento\Framework\Filesystem\Io\Sftp();
        $connection->open(
            ['host' => $host, 'username' => $login, 'password' => $password]
        );

        $connection->cd($dirPath);
        $pwd = $connection->pwd();
        $connection->write($pwd .'/'. $ftpFileName, $filePath);
        $connection->close();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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