2

I have my InstallData but i want to read a file in SQL/CSV format in magento 2

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();

    $tableName = $setup->getTable('custom_table');

        $data = [
            [
                //Read file in sql/CSV format.
            ],

        ];

        $setup
            ->getConnection()
            ->insertMultiple($tableName, $data);

    $setup->endSetup();
}

How do i go about this in magento2...

1 Answer 1

1

You can use the function file_get_contents.

If we suppose that your file is in a directory 'Files' in your module called Test :

$data = file_get_contents($this->directoryList->getModuleDir('','Project_Test').'/Files/file.csv');

where directoryList is Magento\Framework\Module\Dir\Reader;

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.