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.
  • Have you got solution or any query? – Chirag Patel Oct 1 '18 at 13:59
  • no, not yet , still finding the solution – sam Oct 1 '18 at 14:05

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


  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

  • this is the way only i created my controller, but row is not yet skipped. still magento read 2nd line. – sam Oct 1 '18 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. – Chirag Patel Oct 1 '18 at 12:27
  • check this link php.net/manual/en/function.array-slice.php – Chirag Patel Oct 1 '18 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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