How to Get Absolute File Path in Magento 2

Getting the absolute file path is a common work while developing a website or a custom module in Magento 2. So here, we will see how to get the absolute path of a file in Magento 2.

In the following post, to get the absolute path of a file in Magento 2, I will be giving the example for both Dependency Injection (DI) and Object Manager methods. But Magento recommends using the dependency injection method, so try to avoid the object manager method.

In order to get the absolute path of a file in Magento 2, you need to inject the object of \Magento\Framework\Filesystem\DirectoryList class in our module’s class.

Using Object Manager

Here is the example code to get the absolute path of a file in Magento 2 using object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  
$directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
 
echo $directory->getRoot(); // Example Output: /var/www/html/magento_2_3
 
echo $directory->getPath('pub'); // Example Output: /var/www/html/magento_2_3/pub
 
echo $directory->getPath('media'); // Example Output: /var/www/html/magento_2_3/pub/media
 
echo $directory->getPath('upload'); // Example Output: /var/www/html/magento_2_3/pub/media/upload
 
echo $directory->getPath('static'); // Example Output: /var/www/html/magento_2_3/pub/static
 
echo $directory->getPath('app'); // Example Output: /var/www/html/magento_2_3/app
 
echo $directory->getPath('etc'); // Example Output: /var/www/html/magento_2_3/app/etc
 
echo $directory->getPath('di'); // Example Output: /var/www/html/magento_2_3/generated/metadata
 
echo $directory->getPath('generation'); // Example Output: /var/www/html/magento_2_3/generated/code
 
echo $directory->getPath('lib_internal'); // Example Output: /var/www/html/magento_2_3/lib/internal
 
echo $directory->getPath('lib_web'); // Example Output: /var/www/html/magento_2_3/lib/web
 
echo $directory->getPath('setup'); // Example Output: /var/www/html/magento_2_3/setup/src
 
echo $directory->getPath('var'); // Example Output: /var/www/html/magento_2_3/var
 
echo $directory->getPath('cache'); // Example Output: /var/www/html/magento_2_3/var/cache
 
echo $directory->getPath('composer_home'); // Example Output: /var/www/html/magento_2_3/var/composer_home
 
echo $directory->getPath('log'); // Example Output: /var/www/html/magento_2_3/var/log
 
echo $directory->getPath('session'); // Example Output: /var/www/html/magento_2_3/var/session
 
echo $directory->getPath('tmp'); // Example Output: /var/www/html/magento_2_3/var/tmp
 
echo $directory->getPath('view_preprocessed'); // Example Output: /var/www/html/magento_2_3/var/view_preprocessed
 
echo $directory->getPath('html'); // Example Output: /var/www/html/magento_2_3/var/view_preprocessed/html

Using Dependency Injection (DI)

Here is the example code to get the absolute path of a file in Magento 2 using dependency injection. In the dependency injection method, you need to inject the object of \Magento\Framework\Filesystem\DirectoryList class in the constructor of the module’s class as follows,

protected $_directoryList;
...

public function __construct(
    ...
    \Magento\Framework\Filesystem\DirectoryList $directoryList,
    ...
) {
    ...
    $this->_directoryList = $directoryList;
    ...
}

Then, you can get the absolute file path as follows,

$this->_directoryList->getRoot(); // Example Output: /var/www/html/magento_2_3
 
$this->_directoryList->getPath('pub'); // Example Output: /var/www/html/magento_2_3/pub
 
$this->_directoryList->getPath('media'); // Example Output: /var/www/html/magento_2_3/pub/media
 
$this->_directoryList->getPath('upload'); // Example Output: /var/www/html/magento_2_3/pub/media/upload
 
$this->_directoryList->getPath('static'); // Example Output: /var/www/html/magento_2_3/pub/static
 
$this->_directoryList->getPath('app'); // Example Output: /var/www/html/magento_2_3/app
 
$this->_directoryList->getPath('etc'); // Example Output: /var/www/html/magento_2_3/app/etc
 
$this->_directoryList->getPath('di'); // Example Output: /var/www/html/magento_2_3/generated/metadata
 
$this->_directoryList->getPath('generation'); // Example Output: /var/www/html/magento_2_3/generated/code
 
$this->_directoryList->getPath('lib_internal'); // Example Output: /var/www/html/magento_2_3/lib/internal
 
$this->_directoryList->getPath('lib_web'); // Example Output: /var/www/html/magento_2_3/lib/web
 
$this->_directoryList->getPath('setup'); // Example Output: /var/www/html/magento_2_3/setup/src
 
$this->_directoryList->getPath('var'); // Example Output: /var/www/html/magento_2_3/var
 
$this->_directoryList->getPath('cache'); // Example Output: /var/www/html/magento_2_3/var/cache
 
$this->_directoryList->getPath('composer_home'); // Example Output: /var/www/html/magento_2_3/var/composer_home
 
$this->_directoryList->getPath('log'); // Example Output: /var/www/html/magento_2_3/var/log
 
$this->_directoryList->getPath('session'); // Example Output: /var/www/html/magento_2_3/var/session
 
$this->_directoryList->getPath('tmp'); // Example Output: /var/www/html/magento_2_3/var/tmp
 
$this->_directoryList->getPath('view_preprocessed'); // Example Output: /var/www/html/magento_2_3/var/view_preprocessed
 
$this->_directoryList->getPath('html'); // Example Output: /var/www/html/magento_2_3/var/view_preprocessed/html

Read Also: Magento 2: Get Base URL, Store URL and Current URL

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *