Skip to content

“Directory is not under storage root path magento 2

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Greenplank_UploaderFix">
    </module>
</config>

registration.php

<?php
MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    'Greenplank_UploaderFix',
    __DIR__
);

di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="MagentoFrameworkAppFilesystemDirectoryResolver"
                type="GreenplankUploaderFixAppFilesystemDirectoryResolver"/>
</config>

DirectoryResolver.php

<?php
namespace GreenplankUploaderFixAppFilesystem;
 
use MagentoFrameworkAppFilesystemDirectoryList;
 
/**
 * Magento directories resolver.
 */
class DirectoryResolver
{
    /**
     * @var DirectoryList
     */
    private $directoryList;
 
    /**
     * @param DirectoryList $directoryList
     */
    public function __construct(DirectoryList $directoryList)
    {
        $this->directoryList = $directoryList;
    }
 
    /**
     * Validate path.
     *
     * Gets real path for directory provided in parameters and compares it with specified root directory.
     * Will return TRUE if real path of provided value contains root directory path and FALSE if not.
     * Throws the MagentoFrameworkExceptionFileSystemException in case when directory path is absent
     * in Directories configuration.
     *
     * @param string $path
     * @param string $directoryConfig
     * @return bool
     * @throws MagentoFrameworkExceptionFileSystemException
     */
    public function validatePath($path, $directoryConfig = DirectoryList::MEDIA)
    {
        $realPath = realpath($path);
        $root = realpath($this->directoryList->getPath($directoryConfig));
 
        return strpos($realPath, $root) === 0;
    }
}