How to upload a file to an FTP server by coding in Magento 2

Here the following steps help you to upload a file to an FTP server by coding in Magento 2:

Step 1: Build the FTP Connect

Step 2: Use the written function to upload a file to the server

Hi friends. In some cases, some files must be uploaded to another server to save data. For instance, if you want to export the Magento 2 report to shareholders, you have received FTP data from partners, and you can use this tutorial to send a report file to the partners through FTP.

In this article, I will show you How to do it through source code:

  • All you have to use is the function class:

\Magento\Framework\Filesystem\Io\Ftp

So tell the class at the point:

 protected $ftp;

   public function __construct(

       \Magento\Framework\Filesystem\Io\Ftp $ftp

   ){

       $this->ftp = $ftp;

   }
  1. Build the FTP Connect
$open = $this->ftp->open(

               array(

                   'host' => ‘test.localhost.com’,

                   'user' => ‘test’,

                   'password' => ‘test’,

                   'ssl' => true,

                   'passive' => true

               )

           );
  1. Use the written function to upload a file to the server
if ($open) {

               $fileName = ‘test.csv’;

               $content = file_get_contents(DirectoryList::VAR_DIR . '/' . $fileName);

               $this->ftp->write(self::FILE_NAME_ON_FTP, $content);

               $this->ftp->close();

           }

The above mentioned steps are a small process for uploading to the FTP server through the coding in Magento 2. With this guide, you can easily handle the FTP server on Magento 2

Last Update: April 7, 2018  

March 5, 2018   697   Nandini R    Operations    
Total 2 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn