1

I am trying to upload a custom exported CSV to another server FTP on particular location.

Step 1: Added FTP Class in constructor

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

Step 2: Build the FTP Connect

$open = $this->ftp->open(
    array(
        'host' => 'myhostname',
        'user' => 'username',
        'password' => 'password',
        'ssl' => true,
        'passive' => true
    )
);

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

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

The above code is working but I wanted to send file to particular location.

Issue: I want to send file from my server path pub/media/reports to another server path var/www/html/myfoldername/pub/media/reports

Please help me!!

1

You can replace this code in your 3rd step..

if ($open) {
    $fileName = 'myfile.csv';
    $content = file_get_contents(DirectoryList::VAR_DIR . '/pub/media/report' . $fileName);
    $this->ftp->cd("var/www/html/myfoldername/pub/media/reports/");
    $this->ftp->write(self::FILE_NAME_ON_FTP, $content);
    $this->ftp->close();
}

Hope this will help you!

  • Can you please tell me one more thing? there is a variable VAR_DIR it gives var directory path right? – temper Nov 13 '19 at 5:36
  • Yes, Right. @temper – Kishan Savaliya Nov 13 '19 at 5:38
  • var directory which is located inside your Magento root directory. – Kishan Savaliya Nov 13 '19 at 5:41
  • Let me know if this will work for you. – Kishan Savaliya Nov 13 '19 at 5:56
  • 1
    Okay thanks. @temper – Kishan Savaliya Nov 13 '19 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.