I've been writing a rather complicated module as of late. One of the steps I need to do is pre-populate one of the models with all of the data it needs to work right. This data is slowly replaced using a syncing system / rest calls from our database software, but it needs to be there initially so the end user doesn't think its 'all missing' before this sync happens.

I've been trying to use a csv file in my module directory. For the life of me, I can't figure out the combination of factories and methods to get that folder. I can't assume its going to be in ANY particular place though, since this module will end up on a CDN hosted platform.

I've tried injecting


as well as direct php

$_SERVER (ugh)

None of this can figure out


while developing.. and


when testing the composer distribution.

Any suggestions are welcome. I need to load the full path to use something like this....

$csv = file($pathThatWasFound);

EDIT: I read http://alanstorm.com/magento_base_directories <--- based on 1.x magento. I need 2.0 suggestions

EDIT: I may be bumping into constraints of FILE magic and xdebug (https://stackoverflow.com/questions/4924676/how-can-i-get-the-php-magic-constant-file-work-with-eclipse-and-pdt)


The magento2 sample data codebase has good examples of how this is done.

They define the install function in the model to populate the database.

This function is called inside the <vendor_name>\<module_name>\Installer class which is called inside the <vendor_name>\<module_name>\InstallData class.

  • I know how the installer works. Thanks though. I guess I should have mentioned i'm running this function from InstallData.php – CarComp Apr 28 '16 at 19:52

I figured out what I was doing wrong...


I need to use the module namespace as well as the module name on getModuleDir()


This gives me the root path to my module.

My final exact copy of this thing is as follows:

$csv = array_map('str_getcsv',file($readerFactory->getModuleDir('','Stti_Healthday') . '/assignments.csv'))

Depending on what you are doing there are other options


<a href="<?= $block->escapeUrl($block->getViewFileUrl('Xigen_CsvUpload::template.csv'));?>" target="_blank"><?= $block->escapeHtml(__("CSV Template")); ?></a>

So file path is




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.