Used the below method in order to get the media directory path, but it returns an error.

$om = \Magento\Core\Model\ObjectManager::getInstance();

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

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

Please help me to find a solution.

  • 1
    Your question is not clear to me. You can explain more details? Additionally, we can take a look: magento.stackexchange.com/a/155238/33057 Commented May 29, 2017 at 12:32
  • @Rita Jose, does the accepted answer fit in a .phtml file which doesn't have any class? Commented Apr 27, 2020 at 18:35

5 Answers 5


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


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;
  • it returns an error like this " Uncaught TypeError: Argument 2 passed to namespace\Module\Controller\Index\Upload::__construct() must be an instance of Magento\Framework\Filesystem, none given,"
    – Rita Jose
    Commented May 29, 2017 at 12:40
  • yes, try to di:compile and try again :)
    – Keyur Shah
    Commented May 29, 2017 at 12:41
  • di:compile done, but it again returns a errror :( "Recoverable Error: Object of class Magento\Framework\Filesystem\Directory\Read could not be converted to string in /opt/lampp/htdocs/magento214/app/code/namespace/Customtab/Controller/Index/Upload.php on line 18"
    – Rita Jose
    Commented May 29, 2017 at 12:46
  • 1
    see my edit, If you want to use direct object manager @RitaJose
    – Keyur Shah
    Commented May 29, 2017 at 12:47
  • @KeyurShah, Would you please suggest how to get Custom module's image folder directory path? I want to get directory path of my module's image folder.
    – John
    Commented Jun 23, 2017 at 5:01

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:


Other possible uses are:

/* Get app folder */

/* Get configuration folder */

/* Get libraries or third-party components folder */

/* Get libraries/components that need to be accessible publicly through web-server folder */

/* Get public folder */

/* Get static folder */

/* Get var folder */

/* Get temporary files folder */

/* Get file system caching directory (if file system caching is used) */

/* Get logs of system messages and errors */

/* Get file system session directory (if file system session storage is used) */

/* Get directory for Setup application*/

/* Get Dependency injection related file directory */

/* Relative directory key for generated code*/

/* Temporary directory for uploading files by end-user */

/* Directory to store composer related files (config, cache etc.) in case if composer runs by Magento Application */

/* A suffix for temporary materialization directory where pre-processed files will be written (if necessary) */

/* Get template minification dir */
  • it returns browser url of media...i need the folder path of media
    – Rita Jose
    Commented May 29, 2017 at 12:41
  • Please see my updated answer. Commented May 29, 2017 at 12:54
  • till not working. Commented Jun 21, 2018 at 10:24
  • Thank you @MohitKumarArora -- saved my day. the above solution worked like charm
    – Abid Malik
    Commented Mar 7, 2019 at 11:37

Try to get it by using StoreManagerInterface

use Magento\Store\Model\StoreManagerInterface;
protected $storeManager;

public function __construct(
    StoreManagerInterface $storeManager,
    $this->storeManager = $storeManager;

Now get media url using

 $mediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
  • not working..... Commented Jun 21, 2018 at 10:50

Use below code to get the media path on .phtml file.


By Objectmanager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
echo $objectManager->get('Magento\Store\Model\StoreManagerInterface')
  • 3
    It returns the browser url path.. I need the folder path of media
    – Rita Jose
    Commented May 29, 2017 at 12:37

To get media path use below code


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.