4

I tried to create directory and file and then put content in file programmatically but it does not work. Here is my code in sample.phtml

<?php $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Framework\Filesystem $filesystem */ $filesystem = $om->get('Magento\Framework\Filesystem'); $directoryList = $om->get('Magento\Framework\App\Filesystem\DirectoryList'); /** @var \Magento\Framework\Filesystem\Directory\WriteInterface|\Magento\Framework\Filesystem\Directory\Write $writer */ $media_url = getMediaBaseUrl(); $writer = $filesystem->getDirectoryWrite($media_url."module1/"); /** @var \Magento\Framework\Filesystem\File\WriteInterface|\Magento\Framework\Filesystem\File\Write $file */ $contents = "ddddddddddddddddddddddddddd"; $relativeFileName = "sample.txt"; $file = $writer->openFile($relativeFileName, 'w'); try { $file->lock(); try { $file->write($contents); } finally { $file->unlock(); } } finally { $file->close(); } function getMediaBaseUrl() { /** @var \Magento\Framework\ObjectManagerInterface $om */ $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */ $storeManager = $om->get('Magento\Store\Model\StoreManagerInterface'); /** @var \Magento\Store\Api\Data\StoreInterface|\Magento\Store\Model\Store $currentStore */ $currentStore = $storeManager->getStore(); return $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); } ?> 

2 Answers 2

12

Finally, I got a solution for creating files and contents programmatically in Magento 2.

Here is a code

<?php try { $om = \Magento\Framework\App\ObjectManager::getInstance(); $filesystem = $om->get('Magento\Framework\Filesystem'); $directoryList = $om->get('Magento\Framework\App\Filesystem\DirectoryList'); $media = $filesystem->getDirectoryWrite($directoryList::MEDIA); $contents = "ddddddddddddddddddddddddddd"; $media->writeFile("module1/sample.txt",$contents); } catch(Exception $e) { echo $e->getMessage(); } ?> 
5
  • Hello, If i have module xml file then what should i pass into getDirectoryWrite function? Commented Sep 14, 2018 at 12:13
  • you should pass same directory of magento. if your custom module then you have to create folder in media folder. you have to change name "module1" by your module name Commented Sep 22, 2018 at 23:34
  • 1
    Good solution, but you should inject the dependency (only \Magento\Framework\Filesystem needed) into the constructor instead of using the object manager. Instead of $directoryList::MEDIA you can use \Magento\Framework\App\Filesystem\DirectoryList::MEDIA. Commented Apr 27, 2021 at 10:53
  • @Dhrumin can u post the answer or post the link to the answer here? Commented Sep 22, 2021 at 6:50
  • this solution can work with xml file, tried and working good. Commented Sep 22, 2021 at 7:17
1

Url and directory are two different things

You are using

$currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) 

You have to use

$mediaPath=$filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath(); $writer = $filesystem->getDirectoryWrite(mediaPath."module1".DS); 

instead of

$writer = $filesystem->getDirectoryWrite($media_url."module1/"); 
1
  • this code does not work and thanks for the help but I got a solution and post answered Commented May 20, 2018 at 4:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.