How to upload the csv file and read import into the database magento 2?

<?php 

/**
* XXXX_ProductImport extension
* @category 
* @package XXXX_ProductImport
* @copyright Copyright (c) 2018
* @license Open Software License (OSL 3.0)
*/
namespace \ProductImport\Controller\Adminhtml\Imports;

error_reporting(E_ALL);
ini_set('display_errors', 1);

use Magento\Framework\Controller\ResultFactory; 


use Magento\Backend\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\MediaStorage\Model\File\UploaderFactory;

class Save extends \Magento\Backend\App\Action  
{
    protected $fileSystem;

    protected $uploaderFactory;

    protected $allowedExtensions = ['csv']; // to allow file upload types 

    protected $fileId = 'file'; // name of the input file box  

    public function __construct(
       \Magento\Backend\App\Action\Context $context,
        Filesystem $fileSystem,
        UploaderFactory $uploaderFactory
    ) {

        $this->fileSystem = $fileSystem;
        $this->uploaderFactory = $uploaderFactory;
        parent::__construct($context);
    }

    public function execute()
    {

        $destinationPath = $this->getDestinationPath();

       $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        try {
            $uploader = $this->uploaderFactory->create(['fileId' => $this->fileId])
                ->setAllowCreateFolders(true)
                ->setAllowedExtensions($this->allowedExtensions)
                ->addValidateCallback('validate', $this, 'validateFile');


            if (!$uploader->save($destinationPath)) {
                throw new LocalizedException(
                    __('File cannot be saved to path: $1', $destinationPath)
                );
                return $resultRedirect;
            }

            // @todo
            // process the uploaded file
        } catch (\Exception $e) {

            $this->messageManager->addError(
                __($e->getMessage())
            );
            return $resultRedirect;
        }
    }

    public function validateFile($filePath)
    {

        echo "hi";

        // @todo
        // your custom validation code here
    }

    public function getDestinationPath()
    {
        return $this->fileSystem
            ->getDirectoryWrite(DirectoryList::TMP)
            ->getAbsolutePath('/');
    }
}

Here is the easy way to upload file and import/export data by CSV in Magento 2

https://www.magestore.com/magento-2-tutorial/how-to-readwrite-csv-file-from-magento/

To know about CSV files go here:

https://docs.magento.com/m2/2.1/ce/user_guide/system/data-csv.html

Happy Learning.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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