I have created a file uploader on my backend admin. I want to update my products using programmatically by using an csv uploaded.
My controller file is
<?php
namespace Tym17\AdminSample\Controller\Adminhtml\SampleTwo;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends \Magento\Backend\App\Action
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
protected $csv;
/**
* @var scopeConfig
* Needed to retrieve config values
*/
/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory,
ScopeConfigInterface $scopeConfig,
\Magento\Framework\File\Csv $csv
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->scopeConfig = $scopeConfig;
$this->csv = $csv;
}
/**
* Index Action*
* @return void
*/
public function execute()
{
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();
}
}
I have follow this post https://firebearstudio.com/blog/how-to-read-a-csv-file-convert-it-into-an-array-in-magento-2.html
Any help is appreciated.