Upload fails in image uploader - magento2 PHP

Additional Triage information

Summary When uploading an image in the uploader, the error "File validation failed." is shown. This error appeared after updating to 2.2.5.

Preconditions PHP Version 7.0.1 Magento ver. 2.2.5 Make function "mimecontenttype" undefined

Steps to reproduce 1. Edit a block 2. Select the image uploader ("Insert Image" button or the one in the editor) 3. Select an image from your hd and upload it.

Expected result An added image in the gallery

Actual result An error is shown: "File validation failed." No image is uploaded.

I tried different kind of filetypes. In 2.2.4 and previous versions there was no problem, the only thing that changed is the updated version.

Asked Nov 10 '21 17:11
avatar mdesplenter
mdesplenter

6 Answer:

Same problem here after updating to 2.2.5.

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'];

1
Answered Jul 12 '18 at 22:32
avatar  of DevOli
DevOli

I've seen this happening as well after upgrading to Magento 2.2.5. But I've found the reason. We have a custom module which adds pdf files to the allowed file types to upload, and we've defined that like this in the di.xml file of our custom module:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Cms\Model\Wysiwyg\Images\Storage">
        <arguments>
            <argument name="extensions" xsi:type="array">
                <item name="allowed" xsi:type="array">
                    <item name="jpg" xsi:type="number">1</item>
                    <item name="jpeg" xsi:type="number">1</item>
                    <item name="png" xsi:type="number">1</item>
                    <item name="gif" xsi:type="number">1</item>
                    <item name="pdf" xsi:type="number">1</item>
                </item>
                <item name="image_allowed" xsi:type="array">
                    <item name="jpg" xsi:type="number">1</item>
                    <item name="jpeg" xsi:type="number">1</item>
                    <item name="png" xsi:type="number">1</item>
                    <item name="gif" xsi:type="number">1</item>
                    <item name="pdf" xsi:type="number">1</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

But, this now conflicts with Magento 2.2.5 since they've changed that xml in the following commit: https://github.com/magento/magento2/commit/2e3d4776a3398900bcdea0b23b7b0dc13e3a0294#diff-998ea63c2adeec7e9a8ecb860ae11db3

So now instead of defining if the file type is allowed or not with a boolean, you have to specify its mime type in the xml file. If the mime type doesn't match the uploaded file, you'll get a File validation failed. error. This is done in: https://github.com/magento/magento2/blob/2.2.5/app/code/Magento/Cms/Model/Wysiwyg/Images/Storage.php#L491-L493

If you overwrite those allowed filetypes, and don't specify the mime type but set it as 1, like Magento did before 2.2.5, then in 2.2.5 it will cause this problem.

Further looking into this, it also seems like those default values shouldn't even be specified again in your custom module, only new filetypes should be specified, because all the file types get merged (default ones + custom module ones). So in our case, I can fix the problem by changing the di.xml file in our custom module as following:

diff --git a/etc/di.xml b/etc/di.xml
index 48204ce..9c5e8a5 100644
--- a/etc/di.xml
+++ b/etc/di.xml
@@ -4,18 +4,10 @@
         <arguments>
             <argument name="extensions" xsi:type="array">
                 <item name="allowed" xsi:type="array">
-                    <item name="jpg" xsi:type="number">1</item>
-                    <item name="jpeg" xsi:type="number">1</item>
-                    <item name="png" xsi:type="number">1</item>
-                    <item name="gif" xsi:type="number">1</item>
-                    <item name="pdf" xsi:type="number">1</item>
+                    <item name="pdf" xsi:type="string">application/pdf</item>
                 </item>
                 <item name="image_allowed" xsi:type="array">
-                    <item name="jpg" xsi:type="number">1</item>
-                    <item name="jpeg" xsi:type="number">1</item>
-                    <item name="png" xsi:type="number">1</item>
-                    <item name="gif" xsi:type="number">1</item>
-                    <item name="pdf" xsi:type="number">1</item>
+                    <item name="pdf" xsi:type="string">application/pdf</item>
                 </item>
             </argument>
         </arguments>

@engcom-backlog-pb: it's arguably if this is a new bug, because the change in the di.xml file was done in a backwards incompatible way I think (not sure how you would do it in a backwards compatible way). But this is debatable.

I don't expect Magento will change or fix this, so people running into this, should just update their custom modules and specify the mime types in the overwritten allowed filetypes list and remove the default ones from there. (I haven't verified if changing it from a number to a mime type is then still compatible with Magento < 2.2.5, so make sure to double check if your module is installable in various different Magento versions).

1
Answered Jul 07 '18 at 13:02
avatar  of hostep
hostep

im getting this error on the category section. On the content tab after selecting my image for the category i get the exact same error on an image that worked in 2.2.4

1
Answered Jul 04 '18 at 13:55
avatar  of 666triv
666triv

I am facing this issue on Fresh Installation magneto 2.2.5

Apache Server PHP 7.0 Luma Theme There is no custom module. Then how this is not a Bug?

1
Answered Jul 09 '18 at 11:12
avatar  of pieal86
pieal86

The comment from @hostep fixed my issue. There was indeed a module that allowed pdf's to be uploaded, and overwriting all allowed files in the new version of Magento. Thank you!

1
Answered Jul 12 '18 at 11:34
avatar  of mdesplenter
mdesplenter

@DevOli Thanks That solved the issue for me. @engcom-backlog-pb Really didnt like the way it was declared as not a core issue.. but looks like it is. Let me know if u have any solution other than changing in core framework file.

1
Answered Jul 13 '18 at 08:58
avatar  of pieal86
pieal86