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.

5 Answers 5

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

1
  • Yes,my code is workin fine, I think i used wrong password of the sftp and thank for comments. Apr 9, 2019 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";
        }
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";
} 
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();
0

Working code from my project.

$sftp = $obj->create('Magento\Framework\Filesystem\Io\Sftp');
  $open =  $sftp->open(
       array(
           'host' => $sftp_hostname,
           'username' => $sftp_username,
           'password' => $sftp_password,
           'port'=>$sftp_port
       )
    );

    try{
        // Change directory and move to require directory
        $sftp->cd('/enter_your_sftp_file_path_here/');

        //Fetching/Listing all the files.
        $sftp_server_files = $sftp->ls();

        //File path to put files from SFTP Server to our local server
        $filepath = $dir->getRoot() . '/var/import/inventory/';

        foreach ($sftp_server_files as $file) {

            //Copy files from server
            $source = $file['text'];
            if(strpos($source, 'zip') !== false){
                $destination = $filepath.$source;
                $result = $sftp->read($source, $destination);
                if($result == 'true') {
                   $message[] = 'File read from SFTP server : '.$source;
                }
                else
                {
                  $message[] = 'File not able to read from SFTP server : '.$source;
                }
            }
        }
    } catch ( Exception $e) {
        echo $e->getMessage();
    }

Your Answer

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

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