0

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);
}
?>
3

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();
    }
?>
  • Hello, If i have module xml file then what should i pass into getDirectoryWrite function? – Dhrumin Sep 14 '18 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 – Niraj Patel Sep 22 '18 at 23:34
  • ok thanks,but i have find another solution. – Dhrumin Sep 24 '18 at 6:18
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/");
  • this code does not work and thanks for the help but I got a solution and post answered – Niraj Patel May 20 '18 at 4:59

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.