I'm unable to move the generated csv file from my var/report folder to another sftp server.
Please help me.
If you want to Access the CSV file from the var/report directory then move to pub/ .
Now, you should be able to access the CSV file from anywhere within your server using the path http://your_domain/pub/file.csv or http://your_ip_address/pub/file.csv, depending on your server configuration.
Here are the steps, you can follow to move the var/report/filename to another server:
Step1: Create below constructor to the particular class.
public function __construct(
\Magento\Framework\Filesystem\Io\Ftp $ftpConnection
){
$this->ftpConnection = $ftpConnection;
}
Step2: Create a function & add below code:
$open = $this->ftpConnection->open(
array(
'host' => 'hostname',
'user' => 'username',
'password' => 'password',
'ssl' => true,
'passive' => true
)
);
if ($open) {
$fileName = 'report-filename'; // fetch the file name that you want to move
$content = file_get_contents(DirectoryList::VAR_DIR . '/report' . $fileName);
$this->ftpConnection->cd("--another server directory path--");
$this->ftpConnection->write(self::FILE_NAME_ON_FTP, $content);
$this->ftpConnection->close();
}
This will help you to move files from current server to another server.
Note: You can implement the above code to the CRON/controller/event/plugin or existing function to trigger it.