1

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>
  • 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. – Satish Dubariya Feb 9 '19 at 7:25
  • private function _getMimeType() { return $this->fileMime->getMimeType($this->_file['type']); } – Yogesh Pawar Feb 9 '19 at 7:30
  • i am doing this getting error like Attention File 'image/jpeg' doesn't exist – Yogesh Pawar Feb 9 '19 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 – Satish Dubariya Feb 9 '19 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. – Yogesh Pawar Feb 9 '19 at 8:04
0

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

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

0

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

0

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.