I want to read a html file to string that is located in the pub/media directory, how should I do it?


To get the directory, use Magento\App\Filesystem\DirectoryList:

$om = \Magento\Core\Model\ObjectManager::getInstance();
$directoryList = $om->get(\Magento\App\Filesystem\DirectoryList::class);

$pubMediaDir = $directoryList->getPath(\Magento\App\Filesystem\DirectoryList::MEDIA);

In a custom class you will not instantiate the object manager, but instead inject Magento\App\Filesystem\DirectoryList via di.xml, then use it like this:

$pubMediaDir = $this->directoryList->getPath(DirectoryList::MEDIA);

To read the file to a string, use the PHP function \file_get_contents()

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.