0

I should get file (image) in

magento2/pub/media/vendor_name/....

How can i do this? Thanks

2
  • you need to get image from magento2/pub/media/vendor_name/ ? – aravind Jun 4 '19 at 13:42
  • @aravind yes, I need it – Aaron Jun 4 '19 at 13:46
2

You could get it directly using object manager which is not recommended,

$image = 'Your image name';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$folderPath = $objectManager->get('Magento\Store\Model\StoreManagerInterface')
             ->getStore()
             ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
echo $folderPath."vendor/module/".$image;

Block method recommended,

   /* @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

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

    public function myStoreBaseUrls($imagename){

        $storeManager = $this->_storeManager;
        $mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
       $mediaImage = $mediaUrl."vendor/module/".$imagename;
        return $mediaImage;
     }

You can call that function from phtml,

$imageName = 'YourImage name';
$block->getImageUrl($imageName);
0

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