Read input csv file and convert it to array
In your controller file that $postUrl
points to:
protected $csv;
public function __construct(
\Magento\Framework\File\Csv $csv
)
{
$this->csv = $csv;
}
//The function name should match your controller path
public function import($file)
{
if (!isset($file['tmp_name']))
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid file upload attempt.'));
$csvData = $this->csv->getData($file['tmp_name']);
foreach ($csvData as $row => $data) {
if ($row > 0){
//Start your work
}
}
die();
}
Note:
- The
$data
is an array object already. It will look likearray( 0 => 'UPS', 1 => 'One Night Service' ...... )
- I added
if ($row > 0)
to skip first row as it's the attribute row only. - You must
die()
the execution after your job done. I assume your controller is custom made. If you want to override the core controller, please see here.
Ref: https://www.magestore.com/magento-2-tutorial/how-to-readwrite-csv-file-from-magento/
This it the controller file to save/read imported CSV data:
<?php
namespace VendorName\ModuleName\Controller\Adminhtml\Pincode\Import;
use Magento\Framework\App\Filesystem\DirectoryList;
class Save extends \Magento\Backend\App\Action
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Vendor_ModuleName::manage_pincodes';
/**
* Image uploader
*
* @var \Ktpl\BannerSlider\BannerImageUploader
*/
private $csvUploader;
/**
* @var \Magento\Framework\Filesystem
*/
protected $_filesystem;
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* CSV Processor
*
* @var \Magento\Framework\File\Csv
*/
protected $csvProcessor;
/**
* @param Magento\Backend\App\Action\Context $context
* @param Magento\Store\Model\StoreManagerInterface $storeManager
* @param Magento\Framework\Filesystem $filesystem
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\File\Csv $csvProcessor
) {
$this->_filesystem = $filesystem;
$this->_storeManager = $storeManager;
$this->csvProcessor = $csvProcessor;
parent::__construct($context);
}
/**
* Save action
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$data = $this->getRequest()->getPostValue();
$resultRedirect = $this->resultRedirectFactory->create();
try {
if(isset($data['file_upload']['0'])) {
$data['file_upload'] = $data['file_upload']['0']['name'];
}
else {
$data['file_upload'] = null;
}
if(isset($data['file_upload']) && !is_null($data['file_upload']))
{
$this->getCSVUploader()->moveFileFromTmp($data['file_upload']);
$mediaPath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath()
. '<moduleroute_code>/pincode/' . $data['file_upload'];
$importProductRawData = $this->csvProcessor->getData($mediaPath);
$count = 0;
foreach ($importProductRawData as $rowIndex => $dataRow)
{
if($rowIndex > 0)
{
$model = $this->_objectManager->create('<Vendor>\<ModuleName>\Model\Pincode');
$model->loadByPincode($dataRow[0])
->setData('pincode', $dataRow[0])
->setData('is_cod_available', $dataRow[1])
->setData('delivery_time', $dataRow[2])
->setData('delivery_message', $dataRow[3])
->setData('is_active', $dataRow[4])
->save();
$count++;
}
}
$this->messageManager->addSuccess(__('Total %1 pincodes added / updated successfully.', $count));
}
else
$this->messageManager->addError(__('CSV file not uploaded properly, please try again!'));
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
}
return $resultRedirect->setPath('*/pincode/index');
}
/**
* Get image uploader
*
* @return \Ktpl\BannerSlider\BannerImageUploader
*
* @deprecated
*/
private function getCSVUploader()
{
if ($this->csvUploader === null) {
$this->csvUploader = \Magento\Framework\App\ObjectManager::getInstance()->get(
'<VendorName>\<ModuleName>\CsvUploader'
);
}
return $this->csvUploader;
}
}