1

I am working on Magento 2.2.5, need to validate CSV file for import.

  1. How to skip row? like I don't want to save any data of row 2, then how to skip that row.
  2. I am using only one attribute set, so I don't want to fill that in CSV, so how will i make it default from program. Please any one help me.
2
  • Have you got solution or any query? Oct 1, 2018 at 13:59
  • no, not yet , still finding the solution
    – sam
    Oct 1, 2018 at 14:05

1 Answer 1

0

Try this way, it will help you.

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.

For more reference Click here

3
  • this is the way only i created my controller, but row is not yet skipped. still magento read 2nd line.
    – sam
    Oct 1, 2018 at 12:00
  • This is a demo code example you have to use function to skip array value like using array_slice array_pop etc. Oct 1, 2018 at 12:27
  • check this link php.net/manual/en/function.array-slice.php Oct 1, 2018 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.