3

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

11

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();
    }
?>
6
  • Hello, If i have module xml file then what should i pass into getDirectoryWrite function?
    – Dhrumin
    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 Sep 22, 2018 at 23:34
  • ok thanks,but i have find another solution.
    – Dhrumin
    Sep 24, 2018 at 6:18
  • 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. Apr 27, 2021 at 10:53
  • @Dhrumin can u post the answer or post the link to the answer here?
    – fudu
    Sep 22, 2021 at 6:50
0

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 May 20, 2018 at 4:59

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.