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 the first row as it's the attribute row only. - You must
die()
the execution after your job is done. I assume your controller is custom made-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/