{"id":1928,"date":"2021-10-25T04:55:26","date_gmt":"2021-10-25T04:55:26","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-root-directory-path-in-magento-2\/"},"modified":"2025-05-22T09:53:26","modified_gmt":"2025-05-22T04:23:26","slug":"get-root-directory-path-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-root-directory-path-in-magento-2\/","title":{"rendered":"How to Get Root Directory Path in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Are you a Magento developer working on building extensions?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If yes, then you will have to&nbsp;get root directory path in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You will need access to data stored in the root folder which can be obtained using the directory path.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, implement the below solution to get a directory path in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Root Directory Path in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n\n$directory = $objectManager->get('\\Magento\\Framework\\Filesystem\\DirectoryList');\n\necho $rootPath  =  $directory->getRoot();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">or<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">protected $dir;\n\npublic function __construct(\\Magento\\Framework\\Filesystem\\DirectoryList $dir) {\n    $this->dir = $dir;\n}\n\npublic function getRootDirectory()\n{\n    return $this->dir->getRoot();\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Get other directory paths like:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$this->_dir->getRoot();\n\n$this->_dir->getPath('media');\n\n$this->_dir->getPath('pub');\n\n$this->_dir->getPath('static');\n\n$this->_dir->getPath('var');\n\n$this->_dir->getPath('app');\n\n$this->_dir->getPath('etc');\n\n$this->_dir->getPath('lib_internal');\n\n$this->_dir->getPath('lib_web');\n\n$this->_dir->getPath('tmp');\n\n$this->_dir->getPath('cache');\n\n$this->_dir->getPath('log');\n\n$this->_dir->getPath('session');\n\n$this->_dir->getPath('setup');\n\n$this->_dir->getPath('di');\n\n$this->_dir->getPath('upload');\n\n$this->_dir->getPath('generation');\n\n$this->_dir->getPath('view_preprocessed');\n\n$this->_dir->getPath('composer_home');\n\n$this->_dir->getPath('html');<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with the Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you a Magento developer working on building extensions? If yes, then you will have to&nbsp;get root directory path in Magento 2. You will need&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1928","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1928"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1928\/revisions"}],"predecessor-version":[{"id":14196,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1928\/revisions\/14196"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}