4

Getting an error File validation failed. when trying to upload JPG format images from cms block in Magento 2.3 version. File Path : vendor/magento/module-cms/etc/di.xml

    <argument name="extensions" xsi:type="array">
        <item name="allowed" xsi:type="array">
            <item name="jpg" xsi:type="string">image/jpg</item>
            <item name="jpeg" xsi:type="string">image/jpeg</item>
            <item name="png" xsi:type="string">image/png</item>
            <item name="gif" xsi:type="string">image/gif</item>
            <item name="svg" xsi:type="string">image/svg+xml</item>
        </item>
        <item name="image_allowed" xsi:type="array">
            <item name="jpg" xsi:type="string">image/jpg</item>
            <item name="jpeg" xsi:type="string">image/jpeg</item>
            <item name="png" xsi:type="string">image/png</item>
            <item name="gif" xsi:type="string">image/gif</item>
            <item name="svg" xsi:type="string">image/svg+xml</item>
        </item>
        <item name="media_allowed" xsi:type="array">
            <item name="flv" xsi:type="string">video/x-flv</item>
            <item name="swf" xsi:type="string">application/x-shockwave-flash</item>
            <item name="avi" xsi:type="string">video/x-msvideo</item>
            <item name="mov" xsi:type="string">video/x-sgi-movie</item>
            <item name="rm" xsi:type="string">application/vnd.rn-realmedia</item>
            <item name="wmv" xsi:type="string">video/x-ms-wmv</item>
        </item>
    </argument>
11
  • then you should debug in checkMimeType() function in vendor/magento/framework/File/Uploader.php. and print $this->_getMimeType() to see which mimeType you get for your image. Feb 9, 2019 at 7:25
  • private function _getMimeType() { return $this->fileMime->getMimeType($this->_file['type']); } Feb 9, 2019 at 7:30
  • i am doing this getting error like Attention File 'image/jpeg' doesn't exist Feb 9, 2019 at 7:31
  • like this:public function checkMimeType($validTypes = []) { echo $this->_getMimeType();die; if (count($validTypes) > 0) { if (!in_array($this->_getMimeType(), $validTypes)) { return false; } } return true; } and check in inspect element netowork xhr Feb 9, 2019 at 7:32
  • A technical problem with the server created an error. Try again to continue what you were doing. If the problem persists, try again later. Feb 9, 2019 at 8:04

7 Answers 7

3

If you are trying to upload svg image and you have allowed <item name="svg" xsi:type="string">image/svg+xml</item> in vendor/magento/module-cms/etc/di.xml file and still you are getting error File validation failed. then you need to check which MimeType you get for your uploaded image in public function checkMimeType() of this file: vendor/magento/framework/File/Uploader.php. In my case i get image/svg so i need to added <item name="svg" xsi:type="string">image/svg</item> instead of <item name="svg" xsi:type="string">image/svg+xml</item>. If this works then you can override vendor/magento/module-cms/etc/di.xml file.

If still not work and getting {"error":"File 'image\/jpeg' doesn't exist","errorcode":0} in response then check this link: https://github.com/magento/magento2/issues/16531/#issuecomment-404671271

1
  • i am upload jpg image Feb 9, 2019 at 7:31
3

I have experienced the same issue. After installing "PHP Fileinfo module" it works. The issue is fixed.

enter image description here

2

I was able to track my problem to Magento/Framework/File/Uploader.php:

private function _getMimeType()
     {
          return $this->fileMime->getMimeType($this->_file['tmp_name']);
     }

The temporary file name stored in $this->_file['tmp_name'] does not have an extension so Mime type returned will not be correct. I was able to solve my issue by going back and use

return $this->_file['type'];

see https://community.magento.com/t5/Magento-2-x-Technical-Issues/File-validation-failed/td-p/102830

1

Try following code:

    <arguments>
        <argument name="extensions" xsi:type="array">
            <item name="allowed" xsi:type="array">

                <item name="jpg" xsi:type="string">image/jpg</item>
                <item name="jpeg" xsi:type="string">image/jpeg</item>
                <item name="png" xsi:type="string">image/png</item>
                <item name="gif" xsi:type="string">image/gif</item>
                <item name="svg" xsi:type="string">image/svg+xml</item>
            </item>
            <item name="image_allowed" xsi:type="array">
                <item name="jpg" xsi:type="string">image/jpg</item>
                <item name="jpeg" xsi:type="string">image/jpeg</item>
                <item name="png" xsi:type="string">image/png</item>
                <item name="gif" xsi:type="string">image/gif</item>
                <item name="svg" xsi:type="string">image/svg+xml</item>
            </item>
            <item name="media_allowed" xsi:type="array">
                <item name="flv" xsi:type="string">video/x-flv</item>
                <item name="swf" xsi:type="string">application/x-shockwave-flash</item>
                <item name="avi" xsi:type="string">video/x-msvideo</item>
                <item name="mov" xsi:type="string">video/x-sgi-movie</item>
                <item name="rm" xsi:type="string">application/vnd.rn-realmedia</item>
                <item name="wmv" xsi:type="string">video/x-ms-wmv</item>
            </item>
        </argument>
    </arguments>

Hope this will be working.

0
1

"ico" type is associated with "image/vnd.microsoft.icon", This is the part that is not recognized when we upload our ICO file.

In vendor\magento\module-media-storage\Model\File\Validator\Image.php,

replace

“image/vnd.microsoft.icon”

to

“image/x-icon”

But, this is the core file, so you need to override the file or create patch for it.

0

In some cases we have to use return true;,

after using return $this->_file['type']; in the function

public function checkMimeType($validTypes = [])
    {
        if (count($validTypes) > 0) {
            if (!in_array($this->_getMimeType(), $validTypes)) {
                return false;
            }
        }
        return true;
    }
0

I managed to solve this for us by changing the image adapter

In Magento Admin go to Stores > Configuration

Under the Advanced Tab > Developer

You will see Image Processing Settings this was set to PHP GD2

Changing this to ImageMagick allowed me to upload png images

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.