0

I have to read CSV files from an ftp and copy it to my magento2 var folder.

I'm getting this exception Exception #0 (Exception): Unable to open SFTP connection

When I tried with main cpanel user,it seems to be working. But it is not working with ftp login that I created using the main cpanel user.

Pasting the code below.

public function __construct(

................................

\Magento\Framework\Filesystem\Io\Sftp $ftp,

...................................

)

{

..............

$this->ftp = $ftp;

.................

}

 $fthost = 'ip-addreess';
        $ftusername = '[email protected]';
        $ftpwd = 'password';
        $ftport = '21';
    $open = $this->ftp->open(
        array(
            'host' => $fthost,
            'username' => $ftusername,
            'password' => $ftpwd,
            'port' => $ftport,
            'ssl' => true,
            'passive' => true
            )
    );
   // echo 'test';
    //var_dump($this->ftp->ls()); exit;
    //$ftp_open_stat = 1;
    $logger->info('Move File from Server');
    /* MOVE FILE FROM REMOTE TO SERVER */

    $result = $this->ftp->read('/home1/dev/public_html/test/test.csv', '/var/www/html/magetest/var/sync/test.csv');

1 Answer 1

0

You say SFTP but you try to connect using port 21, that's conflicting. Typically ssh connections should go through port 22. Although the class you are using doesn't seem to consider it the way you are specifying it

    /**
     * Open a SFTP connection to a remote site.
     *
     * @param array $args Connection arguments
     *        string $args[host] Remote hostname
     *        string $args[username] Remote username
     *        string $args[password] Connection password
     *        int $args[timeout] Connection timeout [=10]
     * @return void
     * @throws Exception
     */
    public function open(array $args = [])
    {
        if (!isset($args['timeout'])) {
            $args['timeout'] = self::REMOTE_TIMEOUT;
        }
        if (strpos($args['host'], ':') !== false) {
            list($host, $port) = explode(':', $args['host'], 2);
        } else {
            $host = $args['host'];
            $port = self::SSH2_PORT;
        }
        $this->_connection = new \phpseclib3\Net\SFTP($host, $port, $args['timeout']);
        if (!$this->_connection->login($args['username'], $args['password'])) {
            // phpcs:ignore Magento2.Exceptions.DirectThrow
            throw new Exception(
                sprintf("Unable to open SFTP connection as %s@%s", $args['username'], $args['host'])
            );
        }
    }

My bet is you need to specify a real host here: $fthost = 'ip-addreess';

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.