9

How to get media path in Magento 2.1. I tried lots of option but it is not working?

I want to display the image in front-end inside .phtml file.

  • Can you show us what you've tried so far and what were the associated results? – Julien Lachal Sep 15 '16 at 12:30
11

you can get media path by $object method

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
echo $objectManager->get('Magento\Store\Model\StoreManagerInterface')
                    ->getStore()
                    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

factories method

<?php
    namespace Namespace\Modulename\Block;
    use Magento\Framework\ObjectManagerInterface;

    class Banners extends Template
    {

        protected $objectManager;
        public function __construct( 
            ....... 
            ObjectManagerInterface $objectManager
                       ...) {

                    ...........
                    $this->objectManager = $objectManager;

                    ...............
        }
        public function getMediaUrl(){

            $media_dir = $this->objectManager->get('Magento\Store\Model\StoreManagerInterface')
                ->getStore()
                ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

            return $media_dir;
        }
    }
    ........
?>

in phtml file

<?php echo $block->getMediaUrl(); ?>
| improve this answer | |
11

Usage of Object Manager is discouraged as per Magento's coding standards. In a template file, we can get the media URL using the following code:

$this->helper('\Magento\Cms\Helper\Wysiwyg\Images')->getBaseUrl()

As per the Magento's recommendation, the document root of your website should be outside pub directory during development and it should be inside pub directory when moved to production.

Hence we cannot hard code the pub directory while getting media URL in the template files. The above code will give you the correct media URL irrespective of your document root location.

| improve this answer | |
  • 1
    +1 Thinking about adding 20 lines of code for Object Manager sounds horrible just to consider. – Mohammed Joraid May 8 '19 at 14:03
7

Get media path by objectManager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
echo $objectManager->get('Magento\Store\Model\StoreManagerInterface')
                    ->getStore()
                    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

Get media path by Dependency Injection

protected $_storeManager;

public function __construct( 
    ...
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    ...
) {
    ...
    $this->_storeManager = $storeManager;
    ...
}

public function getMediaUrl()
{
    $mediaUrl = $this->_storeManager
                     ->getStore()
                     ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    return $mediaUrl;
}

Now call function getMediaUrl() in phtml

<?php echo $block->getMediaUrl(); ?>
| improve this answer | |
0

$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

Please check this code. It will give you media path.

| improve this answer | |
  • 2
    This does not work in a .phtml file because the _storeManager property is protected – simonthesorcerer Jun 2 '17 at 9:18

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.