How To Upload A File To An FTP Server Via Coding in Magento?

Introduction

Uploading files to FTP servers is vital when you want to store sensitive information. An FTP server is a unique computer dedicated to receiving FTP connections/information using the File Transfer Protocol (FTP) address. FTP is a standard network protocol leveraged when transferring computer files between a server and a client on a specified computer network. The protocol is engineered on a powerful client-server architecture and utilizes separate data connections and controls between the server and the client.

Magento offers reliable FTP access and capability that help you download or upload folders and files from a hosting server or local PC.  There are many options for uploading files to an FTP server in Magento 2, and one of the simplest is using coding. The use of code comes in handy when you want to share critical information such as Magento 2 reports with shareholders and have their FTP addresses.

This tutorial will help you transfer a file to an FTP server in your Magento 2 website using source codes.

Ready? Let’s get started!

The functions we’ll use belong to this class:

\Magento\Framework\Filesystem\Io\Ftp

As a result, you first declare the mentioned class in a construct.

protected $ftp;
  public function __construct(
      \Magento\Framework\Filesystem\Io\Ftp $ftp
  ){
      $this->ftp = $ftp;
  }

Step 1 –
Creating The FTP Connect

An FTP connection is indispensable when uploading files or images to an FTP server. Without this connection, you cannot view or upload files to your server. To create an FTP connection in Magento 2, run the following command:

$open = $this->ftp->open(
               array(
                   'host' => 'test.localhost.com',
                   'user' => 'test',
                   'password' => 'test',
                   'ssl' => true,
                   'passive' => true
               )
           );

This will establish the FTP connection, and you can now commence on transferring the file to an FTP server.

Step 2 –
Uploading The File Using The Write Function

Next, use the written function to upload your file to the FTP server. Run the command below to initiate the transfer:

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();
           }

That’s It!

Conclusion

You have successfully transferred your file to an FTP server using codes. This is a short process that is easy to follow and is one of simplest ways to handle your FTP server on Magento 2.

 

Check out these top 3 Magento hosting services:

HostArmada
$1.79 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
Ultahost
$2.03 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8
Hostinger
$1.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7

How to use Magento 2 CMS

This tutorial is written to help you use Magento 2 Content Management System (CM
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Setup UPS Carrier in Your Magento 2 store

This tutorial will help you configure the United Parcel Service carrier in your
3 min read
Marin Dujic
Marin Dujic
Senior Writer & Hosting Expert

How To setup URL Rewrites In Magento 2

Introduction SEO is a phenomenal online marketing strategy that en
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Add reCAPTCHA to Magento 2

This article will discuss downloading, installing and configuring Magento reCapt
1 min read
Md. Ehsanul Haque Kanan
Md. Ehsanul Haque Kanan
Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top