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
Build your Amazon-like multi-vendor marketplace website @ $249/- now!!!
Get your free live demo now: Click here
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.
How to upload a file to an FTP server by coding in Magento 2?
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; }
Build the FTP Connect
$open = $this->ftp->open( array( 'host' => ‘test.localhost.com’, 'user' => ‘test’, 'password' => ‘test’, 'ssl' => true, 'passive' => true ) );
Use the written function to upload a file to the FTP 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 to transfer to the FTP server through the coding in Magento 2. With this guide, you can easily handle the FTP server on Magento.
Contact us if you still have queries in Magento development related issues.
Leave a Reply