magento2 – Magento 2 how to create custom index.php and pass store code programmatically?

I have a multi-vendor e-commerce platform, everytime I create a new store I need to create a folder for the store and include an index.php

for example:

var/www/html/magento/stores/store2/index.php

Inside the index.php I add the store code like this:

$params = $_SERVER;
$params(MagentoStoreModelStoreManager::PARAM_RUN_CODE) = 'store2_code'; //this is the important line
$params(MagentoStoreModelStoreManager::PARAM_RUN_TYPE) = 'store';
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $params);
/** @var MagentoFrameworkAppHttp $app */
$app = $bootstrap->createApplication('MagentoFrameworkAppHttp');
$bootstrap->run($app);

How could I programmatically create an index.php file and add the store code?
Currently I know how to create the store programmatically, but just the index.php is missing.

Thanks!