---
title: "How to Create Dynamic File while installing Extensions in Magento 2?"
url: "https://magecomp.com/blog/create-dynamic-file-installing-extensions-magento-2/"
date: "2018-07-02T18:15:39+00:00"
modified: "2025-02-21T06:56:57+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 391
reading_time: "2 min read"
summary: "The Magento extension is made up of several files that work together to serve a purpose for it made. This extension either overrides default Magento files or works separately to enhance default Mag..."
description: "The Magento extension is made up of several files that work together to serve a purpose for it made. But sometimes while installing an extension we need dyna..."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Add Product Review on Product Detail Page Programmatically in Magento 2?"
    url: "https://magecomp.com/blog/add-product-review-programmatically-magento-2/"
  - title: "How to Create Condition Rule In Magento admin Form"
    url: "https://magecomp.com/blog/how-to-create-condition-rule-in-magento-admin-form/"
  - title: "How to Setup Reports Configuration in Magento 2"
    url: "https://magecomp.com/blog/setup-reports-configuration-in-magento-2/"
---

# How to Create Dynamic File while installing Extensions in Magento 2?

_Published: July 2, 2018_  
_Author: Dhiren Vasoya_  

![how to Create Dynamic File while installing Extensions magento 2](https://magecomp.com/blog/wp-content/uploads/2018/07/how-to-Create-Dynamic-File-while-installing-Extensions-magento-2-1024x332.webp)

The Magento extension is made up of several files that work together to serve a purpose for it made. This extension either overrides default Magento files or works separately to enhance default Magento functionality by extending the native code.

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-6.webp)](https://magecomp.com/services/hyva-theme-development/)

Every time, when we install the extension inside Magento store, it requires some of the user input before installation and saves to file instead of a database. These files are generally known as a dynamic file which contains dynamic or you can say changeable values instead of any static value. Recently while creating an extension we came across the same requirements.

[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-5.webp)](https://magecomp.com/services/hire-magento-programmer/)

We need to create one file for our requirement of creating a dynamic file using InstallData class inside our Module using below code.
**app\code\Vendor\Extension\Setup\InstallData.php**

```
<?php
namespace VendorExtensionSetup;
use MagentoEavModelConfig;
use MagentoFrameworkAppCacheTypeListInterface;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoFrameworkAppConfigStorageWriterInterface;
use MagentoFrameworkAppFilesystemDirectoryList;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkModuleDir;

class InstallData implements InstallDataInterface
{

  	const XML_PATH_CUSTOMER_FILENAME = 'customer/privacy/cipher_filename';
	protected $scopeConfig;
	protected $configWriter;
	protected $directoryList;
	protected $cacheTypeList;
	protected $eavConfig;

        public function __construct
        (
	    	ScopeConfigInterface $scopeConfig,
	    	WriterInterface $configWriter,
	    	TypeListInterface $cacheTypeList,
	    	DirectoryList $directoryList,
	    	Config $eavConfig,
		Reader $moduleReader
	)
	{
    	$this->scopeConfig = $scopeConfig;
    	$this->configWriter = $configWriter;
    	$this->cacheTypeList = $cacheTypeList;
    	$this->directoryList = $directoryList;
    	$this->eavConfig = $eavConfig;
	}

	public function install( ModuleDataSetupInterface $setup, ModuleContextInterface $context )
	{
    	$setup->startSetup();
    	$dir = $this->getDatabaseDirectory();
  	  if (!is_dir($dir)) {
        	mkdir($dir, 0700, true);
    	}
    	$fileName = $this->scopeConfig->getValue(self::XML_PATH_CUSTOMER_FILENAME);
    	if (strlen($fileName) == 0) {
        	$fileName = md5(microtime(true) . rand(0, 10000));
            $this->configWriter->save(self::XML_PATH_CUSTOMER_FILENAME, $fileName);
            $this->cacheTypeList->cleanType('config');
    	}
    	$file = $dir . '/' . $fileName . '.php';
    	if (!is_file($file)) {
       	 file_put_contents($file, 'Hello Vendor');
        	chmod($file, 0700);
    	}

    	$setup->endSetup();
	}

        public function getDatabaseDirectory()

	{
	    	$viewDir = $this->moduleReader->getModuleDir(
			MagentoFrameworkModuleDir::MODULE_VIEW_DIR,
			'Vendor_Extension'
	    	);
    	        return $viewDir . '/frontend/web';
	}

}
```

Also, you can play manipulate with these codes according to your need of creating dynamic file while creating an extension.

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2-2.webp)](https://magecomp.com/services/magento-upgrade-services/)

Lastly, hit that stars if this code works for you and comment down below if you face any issue while using this code. Happy Coding!


---

_View the original post at: [https://magecomp.com/blog/create-dynamic-file-installing-extensions-magento-2/](https://magecomp.com/blog/create-dynamic-file-installing-extensions-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-09 19:32:35 UTC_  
