3

I have a sample CSV file and want to send it to another server using FTP. I have successfully connected FTP but couldn't able to send the file.

In the FTP where I want to send, there are 2 folders named Import and Export. I have checked those folders but I can't see the file which I sent. I didn't get any error as well. So, I'm not sure, where am I wrong.

In order to send the file using FTP, I have followed the below link:

https://www.magestore.com/magento-2-tutorial/how-to-upload-file-to-ftp-server-by-coding-in-magento-2/

Would anyone please help me to resolve this issue?

Code:

<?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' => 'xyz',
                        'password' => 'pwd',
                        'port' =>22,
                        'passive' => true
                    )
                );

if ($open) {
      $fileName = ‘sample.csv’;
      $content = file_get_contents(DirectoryList::VAR_DIR . '/' . $fileName);
      $this->ftp->write(self::FILE_NAME_ON_FTP, $content);
      $this->ftp->close();
 }

 if(is_null($connection)){
      echo "true";
      //Here is code ....
 }else{
      echo "false";
 }
6
  • Can you please update your code in question ? Commented Dec 24, 2019 at 10:20
  • I have added the code in question. Please have a look and help me.
    – Mohammad
    Commented Dec 24, 2019 at 10:41
  • What is the value in self::FILE_NAME_ON_FTP ? Commented Dec 24, 2019 at 10:44
  • First of all, I have put the filename which is in my local. That wasn't worked. So I have removed that and put the original code as I mentioned in URL which is on question.
    – Mohammad
    Commented Dec 24, 2019 at 10:45
  • 1
    add file name instead of self::FILE_NAME_ON_FTP. and check root folder of the server...Means before public_html Commented Dec 24, 2019 at 10:46

2 Answers 2

4

Please replace your code with below content

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

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

$objectManager = $bootstrap->getObjectManager();

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

$directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');

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

$fileName = 'sample.csv';
$content = file_get_contents($directory->getPath('var') . '/' . $fileName);
$sftp->write('/Import/test1.csv', $content);
$sftp->close();
echo "File uploaded..!";

Hope this will help you!

4
2

Please replace your code with below code and check:

<?php
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;
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' => 'xyz',
                        'password' => 'pwd',
                    )
                );

if ($open) {
      $fileName = 'sample.csv'; // create sample.csv file in var folder
      $content = file_get_contents(DirectoryList::VAR_DIR . '/' . $fileName);
      $sftp->write('yourprojectpath/test.csv', $content); //please give full path of your hosting root project path and create first test.csv file
      $sftp->close();
 } else {
    echo "Something wrong in hosting detail";
 }

please replace code in below core file or override in your module. because open function nothing to return. we need to return true when our hosting details are true

/vendor/magento/framework/Filesystem/Io/Sftp.php

From :

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 \phpseclib\Net\SFTP($host, $port, $args['timeout']);
        if (!$this->_connection->login($args['username'], $args['password'])) {
            throw new \Exception(
                sprintf("Unable to open SFTP connection as %s@%s", $args['username'], $args['host'])
            );
        }
    }

To :

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 \phpseclib\Net\SFTP($host, $port, $args['timeout']);
        if (!$this->_connection->login($args['username'], $args['password'])) {
            throw new \Exception(
                sprintf("Unable to open SFTP connection as %s@%s", $args['username'], $args['host'])
            );
        }
        return true;
    }
9
  • I have tried with your code but still I can't see my file in Server.
    – Mohammad
    Commented Dec 24, 2019 at 11:15
  • any error getting when u ran this script?? Commented Dec 24, 2019 at 11:16
  • Do I need to put the folder name while sending my file since in FTP I have 2 folders?
    – Mohammad
    Commented Dec 24, 2019 at 11:16
  • No, I'm not getting any errors.
    – Mohammad
    Commented Dec 24, 2019 at 11:17
  • can u please add your ftp folder path before 'test.csv' Commented Dec 24, 2019 at 11:18

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.