Question

After upgrading from 2.1.5 to 2.1.6 CE. I'm getting below error on Windows WAMP while saving product on Admin side.

Unable to write file into directory \D:/wamp/www/magento/pub/media/catalog/product\cache\3cf5799449660ed39031217945ace72a/H/3. Access forbidden.

I tried below but it's for Linux

Access Denied (Permission Issue) after upgrade 2.0.3 -> 2.0.4

Was it helpful?

Solution

I just updated to Magento 2.1.6 on Windows and had this same issue. I did a little searching and came across a post on Github here.

In a nutshell you need to do two things in vendor/magento/module-catalog/Model/View/Asset/Image.php.

The first thing is replace all occurrences of DIRECTORY_SEPARATOR with '/'

The second is edit the getRelativePath function to look like:

private function getRelativePath($result)
{
    $prefix = $result == '/' ? $result : '';
    $result = $this->join($result, $this->getModule());
    $result = $this->join($result, $this->getMiscPath());
    $result = $this->join($result, $this->getFilePath());
    return $prefix . $result;
}

You need both changes or it won't work. Just replacing DIRECTORY_SEPARATOR still throws a forbidden error. Not including the $prefix elements in getRelativePath causes the forbidden error to go away, but product images won't load.

OTHER TIPS

There is a simple way to solve this issue: in vendor/magento/module-catalog/Model/View/Asset/Image.php:226 remove the 'DIRECTORY_SEPARATOR'

private function getRelativePath($result)
{
$result = $this->join($result, $this->getModule());
$result = $this->join($result, $this->getMiscPath());
$result = $this->join($result, $this->getFilePath());
return $result;
}

and: /vendor/magento/module-catalog/Model/View/Asset/Image.php:130 replace DIRECTORY_SEPARATOR to '/'

 private function join($path, $item)
    {
        return trim(
            $path . ($item ? '/' . ltrim($item, '/') : ''),
            '/'
        );
    }

PHP accepts both \ and / as valid path separators. So just use / in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top