How to get absolute path of a file in Magento 2

How to get absolute path of a file in Magento 2

While building your custom extension, you will usually need path to your extensions files.

However sometimes you will need to get absolute path to Magento 2 root folder or maybe to media folder.

To directly access those files through your extension you can use Magento\Framework\App\Filesystem\DirectoryList class.

First you will need to inject DirectoryList class into your Magento 2 constructor:

[codesyntax lang=”php”]

...
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;  
    }
...

[/codesyntax]

After that you will have access to DirectoryList methods for retrieving various paths and it will be available to you through:

$this->directory_list

property.

For getting your Magento 2 root folder you can use

$this->directory_list->getRoot();

For getting media folder you can use

$this->directory_list->getPath('media');

 

Other possible uses are:
[codesyntax lang=”php”]

/* 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');

[/codesyntax]

One thought on “How to get absolute path of a file in Magento 2”

  1. Cool example, except it’s usually preferable to use the getPath() function like this:

    $this->directoryList->getPath(DirectoryList::VAR_DIR)

    Or if you prefer to use the full class:

    $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR)

    Also, be advised, Magento itself uses camelcasing and not underscores, so it’s probably best to stick to that coding standard.

Leave a Reply

Your email address will not be published.