11

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.

1
  • Can you show us what you've tried so far and what were the associated results? Commented Sep 15, 2016 at 12:30

5 Answers 5

13

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(); ?>
1
  • Please note, for recent versions (Magento 2.1+), using ObjectManager is discouraged, please see more relevant answers below for getting it in more recommended ways. Commented Oct 19, 2021 at 11:42
19

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.

1
  • 1
    This seems to be the best answer I have come across 👍👍
    – 00-BBB
    Commented Aug 5, 2021 at 8:51
8

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(); ?>
2

Don't use objectManager

Magento 2. Get media URL in template file

You can get media url in your template file using below way but without using objectmanager you must have to define Block file with __construct() method with define storeManagerInterface in construct method.

In your phtml Block file create __construct function.

Add to __construct in your class


protected $_storeManager;

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

Add new method in your class

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

Call in phtml


  <img src="<?php echo $block->getMediaUrl('wysiwyg/img.jpg') ?>" />

This is the +- proper way to get media url in Magento 2.

0

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

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

1
  • 2
    This does not work in a .phtml file because the _storeManager property is protected Commented Jun 2, 2017 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.