Skip to main content

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:

  1. The $data is an array object already. It will look like array( 0 => 'UPS', 1 => 'One Night Service' ...... )
  2. I added if ($row > 0) to skip the first row as it's the attribute row only.
  3. 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/

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:

  1. The $data is an array object already. It will look like array( 0 => 'UPS', 1 => 'One Night Service' ...... )
  2. I added if ($row > 0) to skip first row as it's the attribute row only.
  3. 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/

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:

  1. The $data is an array object already. It will look like array( 0 => 'UPS', 1 => 'One Night Service' ...... )
  2. I added if ($row > 0) to skip the first row as it's the attribute row only.
  3. You must die() the execution after your job is 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/

Source Link
PY Yick
  • 2.7k
  • 1
  • 18
  • 35

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:

  1. The $data is an array object already. It will look like array( 0 => 'UPS', 1 => 'One Night Service' ...... )
  2. I added if ($row > 0) to skip first row as it's the attribute row only.
  3. 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/