0
public function execute()
{   
    $data = [];
    $file = $this->getRequest()->getFiles('refundcsv');
    print_r($file);//display the input file name 
    if (!isset($file)) 
    {
        throw new \Magento\Framework\Exception\LocalizedException(__('Invalid file upload attempt.'));
    }
    $data = $this->csv->getData($file); //throws error
    print_r($data);
    foreach ($csvData as $row => $data)
    {
      if ($row > 0)
      {
         print_r($row);
      }
    }
    die();
}

I want to read the csv input file and validate the rows. While executing the above code shows an error.

Warning: file_exists() expects parameter 1 to be a valid path, array given in vendor/magento/framework/File/Csv.php

2 Answers 2

0

Please check the below code,

            $csvs  = $this->request->getFiles('importdata');
            if ((isset($csvs['name'])) && ($csvs['name'] != '')) {
                $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'importdata']);
                $uploaderFactory->setAllowedExtensions(['csv', 'xls']);
                $uploaderFactory->setAllowRenameFiles(true);
                $uploaderFactory->setFilesDispersion(true);

                $mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
                $destinationPath = $mediaDirectory->getAbsolutePath('FOLDER_PATH');
                $result = $uploaderFactory->save($destinationPath);
                if (!$result) {
                    $this->messageManager->addSuccess(__('File cannot be saved to path: $1', $destinationPath));
                    $this->_redirect('..path..');
                } else {
                    $imagePath = 'FOLDER_PATH' . $result['file'];
                    $mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
                    $destinationfilePath = $mediaDirectory->getAbsolutePath($imagePath);
                    $csv_data = $this->csvProcessor->getData($destinationfilePath);
                    $headerArray = ["name", "sku", "desc"];
                    $headers = $csv_data[0];
                    $csvHeaders = false;

                    if (count(array_intersect($headerArray, $headers)) != count($headerArray)) {
                        $this->messageManager->addError(__("Invalid Formated File"));
                        $this->_redirect('...path...');
                    } else {
                        $count = 0;
                        foreach ($csv_data as $importItem) {
                            if (!$csvHeaders) {
                                $csvHeaders = $importItem;
                                continue;
                            }
                            $mapArr['name'] = $importItem[array_search('name', $headers)];
                            $mapArr['sku'] = $importItem[array_search('sku', $headers)];
                            $mapArr['desc'] = $importItem[array_search('desc', $headers)];
                             ....

                             ....
                            $count++;

                        }
                        $this->messageManager->addSuccess(__('A total of %1 record(s) have been Added.', $count));
                    }
                    $this->_redirect('..path...');
                }
              }

my csv file saved in meida/FOLDER_PATH

5
  • In my case, I am getting csv input from html form. How to read that. I am facing this error while reading (Warning: file_exists() expects parameter 1 to be a valid path, array given in vendor/magento/framework/File/Csv.php)
    – Mythili
    Commented Dec 20, 2021 at 6:15
  • @Mythili Try to save the csv file to a folder. Then, get the file from there and read it.
    – ajay
    Commented Dec 20, 2021 at 7:34
  • How to validate row? If the user fills one column value in CSV and another one is empty. How to do that?
    – Mythili
    Commented Dec 21, 2021 at 12:06
  • In the above code "$mapArr['name'] = $importItem[array_search('name', $headers)];" get the value of column name row values. You can validate this by isset or empty with $mapArr['name'] values in the for loop for each row. I hope it helps you.
    – ajay
    Commented Dec 25, 2021 at 7:15
  • 1
    Thank you...It helps a lot
    – Mythili
    Commented Dec 27, 2021 at 14:14
0

To read csv file try this:

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();
}

Your Answer

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

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