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!!

asked Nov 13, 2019 at 4:54

1 Answer 1

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!

answered Nov 13, 2019 at 5:35
12
  • Can you please tell me one more thing? there is a variable VAR_DIR it gives var directory path right? Commented Nov 13, 2019 at 5:36
  • Yes, Right. @temper Commented Nov 13, 2019 at 5:38
  • var directory which is located inside your Magento root directory. Commented Nov 13, 2019 at 5:41
  • Let me know if this will work for you. Commented Nov 13, 2019 at 5:56
  • I am doing this in cron and everytime cron gives success result, but no file transferred to FTP. Commented Nov 13, 2019 at 5:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.