图片上传报错
后台图片上传或者创建图片储存文件夹时报错:
Directory D:/softe/www/bm.local.com/pub/media/wysiwyg is not under storage root path.
这是由于windows环境下获取"root"路径时没有将 '\'转换成'/'导致的具体报错原因体现在:
D:\softe\www\bm.local.com\vendor\magento\framework\App\Filesystem\DirectoryResolver.php
文件中的validatePath
方法的root
文件路径没被正确转换
public function validatePath($path, $directoryConfig = DirectoryList::MEDIA)
{
$realPath = realpath($path);
$root = $this->directoryList->getPath($directoryConfig);
return strpos($realPath, $root) === 0;
}
修复
方法一(不推荐,手动修改基础核心文件可能会出现错误)
直接修改原文件将root
地址也使用realpath()
进行转换
public function validatePath($path, $directoryConfig = DirectoryList::MEDIA)
{
$realPath = realpath($path);
$root = realpath($this->directoryList->getPath($directoryConfig));
return strpos($realPath, $root) === 0;
}
方法二(推荐)
使用composer
安装补丁 命令:
- 安装
composer-patches
composer require cweagans/composer-patches
- 保存补丁文件 补丁/ 13929_2.2.3_directory_resolver_composer_v1.patch
--- /vendor/magento/framework/App/Filesystem/DirectoryResolver.php 2018-02-21 01:25:30.000000000 +0000
+++ /vendor/magento/framework/App/Filesystem/DirectoryResolver.php 2018-06-02 17:04:53.000000000 +0000
@@ -39,7 +39,7 @@
public function validatePath($path, $directoryConfig = DirectoryList::MEDIA)
{
$realPath = realpath($path);
- $root = $this->directoryList->getPath($directoryConfig);
+ $root = realpath($this->directoryList->getPath($directoryConfig));
return strpos($realPath, $root) === 0;
}
- 修改composer.json
"extra": {
"magento-force": "override",
"patches": {
"magento/framework": {
"Issue #13929: Images can't be uploaded using WYSIWYG if media directory is a symlink": "./patches/13929_2.2.3_directory_resolver_composer_v1.patch"
}
}
}
4.修补它!
composer install
方法三
重写DirectoryResolver.php
etc/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="Magento\Framework\App\Filesystem\DirectoryResolver"
type="\ModuleVendor\ModuleName\App\Filesystem\DirectoryResolver"/>
</config>
Override/App/Filesystem/DirectoryResolver.php
<?php
declare(strict_types=1);
/**
* TODO: temporary override until patch or Magento 2.3 is released.
* https://github.com/magento/magento2/issues/13929
*/
namespace ModuleVendor\ModuleName\Override\App\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;
/**
* 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 \Magento\Framework\Exception\FileSystemException in case when directory path is absent
* in Directories configuration.
*
* @param string $path
* @param string $directoryConfig
* @return bool
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function validatePath($path, $directoryConfig = DirectoryList::MEDIA)
{
$realPath = realpath($path);
// BEGIN EDIT by @erikhansen
/**
* Since media directory is a symlink, need to run both paths through realpath in order for the comparison to
* work.
* The proper fix for this should involve a STORE > Configuration setting where an admin can choose whether to
* allow symlinked directories.
*/
$root = realpath($this->directoryList->getPath($directoryConfig));
// END EDIT
return strpos($realPath, $root) === 0;
}
}