14

I have created a custom module in which I want to check if a (image)file exists in my view/frontend/web/images folder.

I have checked this answer but in only shows how to get Magento's directory paths.

How can I get full path for my module's directory which will support with Magento 2's static content deploy feature?

EDIT

As per Rakesh's answer I can use below code.

$mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::APP)->getAbsolutePath();
$modulePath = echo $mediapath.'code/Packagename/Modulename/view/frontend/web/images';

Is there any other alternative to this approach?

5
  • Alternativa way is look like pass path in function, $mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::APP)->getAbsolutePath('code/Packagename/Modulename/view/frontend/web/images'); Jun 23, 2017 at 5:59
  • @RakeshJesadiya, I am looking something like we use to get URL e.g. $block->getViewFileUrl('Vendor_Module/images/blank.png');
    – John
    Jun 23, 2017 at 6:04
  • Are you looking for path or url? Jun 23, 2017 at 6:06
  • I am looking for path. I just provided example how Magento gets URL. So is there any code like that which can give us path just like getting URL.
    – John
    Jun 23, 2017 at 6:08
  • Check answer with example, rakeshjesadiya.com/… Nov 21, 2019 at 12:12

4 Answers 4

25

Use code below to get module directory path

class CustomModel
{
    /**
     * @var \Magento\Framework\Module\Dir\Reader
     */
    protected $moduleReader;

    /**
     * @param \Magento\Framework\Module\Dir\Reader $moduleReader
     */
    public function __construct(
        \Magento\Framework\Module\Dir\Reader $moduleReader
    ) {
        $this->moduleReader = $moduleReader;
    }

    public function getDirectory()
    {
        $viewDir = $this->moduleReader->getModuleDir(
            \Magento\Framework\Module\Dir::MODULE_VIEW_DIR,
            'Vendor_Module'
        );
        return $viewDir . '/frontend/web/images';
    }
}

but if you need to check content static file I'd recommend you to use this way...

class CustomModel
{
/**
     * @var \Magento\Framework\View\Asset\Repository
     */
    protected $assetRepository;

    /**
     * @param \Magento\Framework\View\Asset\Repository $assetRepository
     */
    public function __construct(
        \Magento\Framework\View\Asset\Repository $assetRepository
    ) {
        $this->assetRepository = $assetRepository;
    }

    public function getMyFilePath()
    {
        $fileId = 'Vendor_Module::images/myimage.png';
        $params = [
            'area' => 'frontend'
        ];
        $asset = $this->assetRepository->createAsset($fileId, $params);
        try {
            return $asset->getSourceFile();
        } catch (\Exception $e) {
            return null;
        }
    }
}
3
  • Thanks Nicholas. This seems a better approach than using full directory path.
    – John
    Jun 23, 2017 at 6:05
  • 1
    if you need to check content static file, I'd recommend another one way. Added to answer. Jun 23, 2017 at 6:16
  • Sorry for delay, I was just implementing it with my code. Your answer is exactly what I was looking for. Thanks for sharing.
    – John
    Jun 23, 2017 at 6:33
2

You can get module's images path like this: $block->getViewFileUrl('VendorName_ModuleName::images/image.jpg).

1
  • Underrated answer. Easy and clean
    – baoutch
    Mar 10, 2023 at 8:42
2

You can also use Magento\Framework\Module\Dir Class to get Directory path of the module:

EXample:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $dir = $objectManager->get('Magento\Framework\Module\Dir');
    /*Retrieve full path to a directory of certain type within a module*/
    echo $dir->getDir('Magento_Customer');
    echo $dir->getDir('Magento_Customer','Controller');

Syntax:

    getDir('Module_Name','Directories');                                      
    /*getDir method not allow to read type of module's directory except these*/
    const MODULE_ETC_DIR = 'etc';
    const MODULE_I18N_DIR = 'i18n';
    const MODULE_VIEW_DIR = 'view';
    const MODULE_CONTROLLER_DIR = 'Controller';
1

First just get your App folder path after getting app path you can got it by add your sub path,

  use Magento\Framework\App\Filesystem\DirectoryList;

    protected $filesystem;

    public function __construct(
        \Magento\Framework\Filesystem $filesystem
    )
    {
        $this->_filesystem = $filesystem;
    }

    public function getPathofmodule(){
        $mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::APP)->getAbsolutePath();
        $modulePath = echo $mediapath.'code/Packagename/Modulename/view/frontend/web/images';
    }
4
  • Thanks Rakesh. This is what I thought too. But isn't there any other, better way to do it instead of using full directory path?
    – John
    Jun 23, 2017 at 5:40
  • @RakeshJesadiya How to access this image in web,after getting the absolute path ? Dec 28, 2018 at 6:11
  • @AdityaShah rakeshjesadiya.com/… answer in details. Nov 21, 2019 at 12:12
  • Note that if you plan to package your module in order to use it with composer, this solution won't work, because the module will be located in the vendor folder.
    – Cladiuss
    Oct 14, 2020 at 9:56

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.