cancel
Showing results for 
Search instead for 
Did you mean: 

Copy Generated Report to Folder in Magento2

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Copy Generated Report to Folder in Magento2

Hi,

I am generating reports with my code. CSV download option is working fine but file is not copying to the mentioned location. Can any body suggest solution for this. Below is the code in my controller.

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$vendorId = $objectManager->create('Magento\Customer\Model\Session')->getVendorId();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$base_url= $storeManager->getStore()->getBaseUrl();
$file_path = $this->directory_list->getPath('media').'/uploads/'.$vendorId.'/reports/';
$mode = 0755;if(!is_dir($file_path)) {mkdir($file_path,0777,true);}
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 16; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$filename = 'vendor_report_'.$randomString.'_'.time().'.CSV';

$content = $this->reportProduct->getCSvData();
$new_file=$this->_fileFactory->create($filename, $content, DirectoryList::VAR_DIR);
copy($new_file,$file_path.$filename);
//$cpfilePath = $this->_fileFactory->cp($new_file, $file_path.$filename);
return $new_file;