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 Answer 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_DIRit gives var directory path right?temper– temper2019年11月13日 05:36:57 +00:00Commented Nov 13, 2019 at 5:36 -
Yes, Right. @temperKishan Savaliya– Kishan Savaliya2019年11月13日 05:38:35 +00:00Commented Nov 13, 2019 at 5:38
-
var directory which is located inside your Magento root directory.Kishan Savaliya– Kishan Savaliya2019年11月13日 05:41:37 +00:00Commented Nov 13, 2019 at 5:41
-
Let me know if this will work for you.Kishan Savaliya– Kishan Savaliya2019年11月13日 05:56:40 +00:00Commented Nov 13, 2019 at 5:56
-
I am doing this in cron and everytime cron gives success result, but no file transferred to FTP.temper– temper2019年11月13日 05:57:33 +00:00Commented Nov 13, 2019 at 5:57
Explore related questions
See similar questions with these tags.