magento2图片上传无法创建文件夹和上传图片

图片上传报错

后台图片上传或者创建图片储存文件夹时报错:

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安装补丁 命令:

  1. 安装composer-patches
composer require cweagans/composer-patches
  1. 保存补丁文件 补丁/ 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;
     }
  1. 修改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

  1. 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>
  1. 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;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值