How to get media directory path in a phtml file in magento 2?
Instead of using direct object manager
, use It like
use Magento\Framework\App\Filesystem\DirectoryList;
protected $_filesystem;
public function __construct(
\Magento\Framework\Filesystem $filesystem
)
{
$this->_filesystem = $filesystem;
}
Now you can media path by,
$mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
EDIT
If you want to use an Object Manager, then you can use this (not recommended)
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$fileSystem = $objectManager->create('\Magento\Framework\Filesystem');
$mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();
echo $mediaPath;
exit;
First you will need to inject DirectoryList class into your Magento 2 constructor:
public function __construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\App\Filesystem\DirectoryList $directory_list, array $data = []) {
parent::__construct($context, $data);
$this->directory_list = $directory_list;
}
After that you will have access to DirectoryList methods for retrieving various paths. For example, to get media folder you can use:
$this->directory_list->getPath('media');
Other possible uses are:
/* Get app folder */
$this->directory_list->getPath('app');
/* Get configuration folder */
$this->directory_list->getPath('etc');
/* Get libraries or third-party components folder */
$this->directory_list->getPath('lib_internal');
/* Get libraries/components that need to be accessible publicly through web-server folder */
$this->directory_list->getPath('lib_web');
/* Get public folder */
$this->directory_list->getPath('pub');
/* Get static folder */
$this->directory_list->getPath('static');
/* Get var folder */
$this->directory_list->getPath('var');
/* Get temporary files folder */
$this->directory_list->getPath('tmp');
/* Get file system caching directory (if file system caching is used) */
$this->directory_list->getPath('cache');
/* Get logs of system messages and errors */
$this->directory_list->getPath('log');
/* Get file system session directory (if file system session storage is used) */
$this->directory_list->getPath('session');
/* Get directory for Setup application*/
$this->directory_list->getPath('setup');
/* Get Dependency injection related file directory */
$this->directory_list->getPath('di');
/* Relative directory key for generated code*/
$this->directory_list->getPath('generation');
/* Temporary directory for uploading files by end-user */
$this->directory_list->getPath('upload');
/* Directory to store composer related files (config, cache etc.) in case if composer runs by Magento Application */
$this->directory_list->getPath('composer_home');
/* A suffix for temporary materialization directory where pre-processed files will be written (if necessary) */
$this->directory_list->getPath('view_preprocessed');
/* Get template minification dir */
$this->directory_list->getPath('html');
Use below code to get the media path on .phtml file.
$this->getUrl('pub/media');
By Objectmanager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
echo $objectManager->get('Magento\Store\Model\StoreManagerInterface')
->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);