Magento2: [Magento 2.2.5 - 2.3.x] Upload fails in image uploader

Created on 4 Jul 2018  ·  59Comments  ·  Source: magento/magento2

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 "mime_content_type" 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.

FrameworFile dmcdindia1 Fixed in 2.4.x Clear Description Confirmed Format is valid Ready for Work done Reproduced on 2.3.x

Most helpful comment

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

All 59 comments

Hi @mdesplenter. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me {$VERSION} instance

where {$VERSION} is version tags (starting from 2.2.0+) or develop branches (2.2-develop +).
For more details, please, review the Magento Contributor Assistant documentation.

@mdesplenter do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

@magento-engcom-team give me 2.2.5 instance

Hi @mdesplenter. Thank you for your request. I'm working on Magento 2.2.5 instance for you

Hi @mdesplenter, here is your Magento instance.
Admin access: https://i-16531-2-2-5.engcom.dev.magento.com/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.

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

@mdesplenter, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. Please provide more detailed steps to reproduce or try to reproduce this issue on a clean installation or latest release.

I tried uploading an image in the category section and it worked but this is a 2.2.6-dev instance. On 2.2.5 it gives the error. And this applies to any category you upload an image to.

@666triv we are testing on clean 2.2.5 installation and all goes well. So we need more detailed steps to reproduce.

Does the validator need some database entries that might not been added in the upgrade from 2.2.4?
Tomorrow I'll try the 2.2.6-dev version, can I update this with composer?

Same issue for me.
magento 2.2.5
I have used both nginx and apache.
PHP version 7.0
On Local env: nginx everything works fine..
On Server: Apache gives error "File validation Failed" when insert image in static blocks

screenshot_2018-07-05 new block blocks elements content magento admin

@mdesplenter you can ask bot for new Magento instance and test on it.

We are also experiencing the same issue on two instances of Magento 2.1.14. This has only occurred since we recently upgraded to this version of Magento.

I have tried to upload different types of images via the image uploader on pages/blocks and also to categories and each time receive the same "File Validation Failed" error.

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).

@hostep thank you for investigation. Closing as not a bug.

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?

@pieal86 : I've tested this on a fresh installation of 2.2.5 and couldn't reproduce the bug.
In case you can, can you please clarify the exact steps you've taken.

Thanks! :)

Steps:

  1. Download Magento 2.2.5 From Magento site
  2. Upload using Cpanel
  3. Install using Magneto setup installer.
  4. Create CMS page
  5. In Content add Image.
  6. When upload image gives Error: "File validation Failed"

I Tested on local Env which is Nginx.
Installed using Composer. And there is no error.

Hi,

I'm having exactly the same issue.

  • Magento 2.2.5 fresh install
  • Infortis Ultimo theme applied who say it is a Magento issue and can't do anything
  • PHP 7.1
  • Apache

I am seeing this issue as well after updating to 2.2.5

Please proceed to the Community Forums or the Magento Stack Exchange site for advice or general discussion about this issue. The GitHub issue tracker is intended for Magento Core technical issues only.

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!

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

Thanks @DevOli :)

Your solution works for me.

Magento 2.2.5 + Apache

@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.

@DevOli : just out of curiosity: are you uploading files without an extension, or is $this->_file['tmp_name'] always without a file extension with every file you try to upload?
If that last thing is the case, then this sounds like a proper bug indeed, but it would be great if you could try to figure out how or why that happens, because I don't think everyone has that problem.

Thanks, @DevOli. Your solution worked for me too.

Funny. I have two Magento 2.2.5 w/ Apache websites on two different servers. One had this problem and the other did not.

So I think the reason why it won't work for some people will depend on the PHP installation having the mime_magic extension off and with it the function "mime_content_type" undefined (That will be my case). You can see this in Magento/Framework/File/Mime.php. The code has a fallback in case the function is not defined but it will not work because my temporary files do not have extensions even tho the original file name I want to upload has the correct extensions (I think this is general for everyone).

The following function in Mime.php will always return an empty string.

private function getFileExtension(string $file): string
    {
        return strtolower(pathinfo($file, PATHINFO_EXTENSION));
    }

If you want to check if this is your case you can create a php file and run this:

if (function_exists('mime_content_type')) {
            echo '<br>Function mime_content_type is defined';
        } else echo '<br>and Function mime_content_type is not defined';

@DevOli thanks it fixed for me too. So whats the plan on fixing this in the future ?

@engcom-backlog-pb : I'm re-opening this for re-evaluation if that's ok for you, please see the above comment of @DevOli

@DevOli solution didn't work for me.

I had to change the following:

if (count($file) > 0 && count($file[0]) > 0 && count($file[1]) > 0) {
                array_shift($file);
                $this->_uploadType = self::MULTIPLE_STYLE;

                $fileAttributes = $_FILES[$file[0]];
                $tmpVar = [];

                foreach ($fileAttributes as $attributeName => $attributeValue) {
                    $tmpVar[$attributeName] = $attributeValue[$file[1]];
                }

                $fileAttributes = $tmpVar;
                $this->_file = $fileAttributes;
            } elseif (count($fileId) > 0 && isset($_FILES[$fileId])) {
                $this->_uploadType = self::SINGLE_STYLE;
                $this->_file = $_FILES[$fileId];
            } elseif ($fileId == '') {
                throw new \Exception('Invalid parameter given. A valid $_FILES[] identifier is expected.');
            }

To this:

if (count($file) > 0 && count($file[0]) > 0 && count($file[1]) > 0) {
                array_shift($file);
                $this->_uploadType = self::MULTIPLE_STYLE;

                $fileAttributes = $_FILES[$file[0]];
                $tmpVar = [];

                foreach ($fileAttributes as $attributeName => $attributeValue) {
                    $tmpVar[$attributeName] = $attributeValue[$file[1]];
                }

                $fileAttributes = $tmpVar;
                $this->_file = $fileAttributes;

                $ext = pathinfo($this->_file['name'], PATHINFO_EXTENSION);
                $newName = $this->_file['tmp_name'].'.'.$ext;
                rename($this->_file['tmp_name'], $newName);
                $this->_file['tmp_name'] = $newName;



            } elseif (count($fileId) > 0 && isset($_FILES[$fileId])) {
                $this->_uploadType = self::SINGLE_STYLE;
                $this->_file = $_FILES[$fileId];
                $ext = pathinfo($this->_file['name'], PATHINFO_EXTENSION);
                $newName = $this->_file['tmp_name'].'.'.$ext;
                rename($this->_file['tmp_name'], $newName);
                $this->_file['tmp_name'] = $newName;

            } elseif ($fileId == '') {
                throw new \Exception('Invalid parameter given. A valid $_FILES[] identifier is expected.');
            }

Thanks @DevOli, I had to contact our hosting provider and ask them to check on the Fileinfo PHP module extension which in our case wasn't enabled. Turning this on resolved the issue for us.

I too can confirm that moving from 2.2.4 to 2.2.5 produced this bug on apache

I too can confirm that moving from 2.2.4 to 2.2.5 produced this bug on apache

@mdesplenter, thank you for your report.
We've acknowledged the issue and added to our backlog.

enabling Fileinfo php module worked for me

Yes FileInfo PHP module fixed this for me.

I think the compatibility checker needs to check against the required PHP modules for the version you are about to update to. Not your current version.

This way we'd all be made aware that changes are required prior to upgrade.

@AaronONeill Thanks, we installed the FileInfo php module in WHM-cpannel and it resolved this issue.

PHP Fileinfo module installation fixed it for us - many thanks for the solution

Enabling fileinfo solved it for us .... there should be a check during setup.

installing PHP extension php_fileinfo fixed the issue.

Hi @engcom-backlog-nazar. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if your want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.

Guys the issue is with php module fileinfo missing. it should check this extension while installing magento.

Hi @progreg. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if your want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.

Same issue in v2.3 and we have fileinfo extension installed and enabled in php.ini

Hi @vijay-wagento. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.

I am working on this at #dmcdindia19”

@magento-engcom-team give me 2.3-develop instance

Hi @shubhamkhandelwalrtpl. Thank you for your request. I'm working on Magento 2.3-develop instance for you

Hi @shubhamkhandelwalrtpl, here is your Magento instance.
Admin access: https://i-16531-2-3-develop.instances.magento-community.engineering/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.

Also experiencing this issue on 2.3.2. Some more information; in file upload popup windows in the admin panel, the folder tree on the left side fails to show any files when clicking into any folder. Then you see errors:
image

I made sure all my permissions were in order.

edit: installing the fileinfo php extension fixed the issue. thx hostep

if changing mime type doesn't work, you can change the return value of the following function.That is the actual function causing this issue.Make sure following function returns true.

Magento/Framework/File/Uploader.php:

public function checkMimeType($validTypes = [])
    {   
        return true; //we added, now you should upload files
        if (count($validTypes) > 0) {
            if (!in_array($this->_getMimeType(), $validTypes)) {
                return false;
            }
        }
        return true;
    }

Hi @madbounce. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [x] 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


Hey, checked current issue on branch 2.3-develop and issue currently not reproduced.

@madbounce: it's still an issue as far as I know if you do not have the fileinfo php extension installed which isn't a strict requirement for Magento2. A fix is currently being worked on in https://github.com/magento/magento2/pull/24455 (although it doesn't seem to be going very well)

Hi @engcom-Delta. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

Issue is reproducible on 2.3-develop branch: php7.2 without fileinfo php extension
#16531issue

:white_check_mark: Confirmed by @engcom-Delta
Thank you for verifying the issue. Based on the provided information internal tickets MC-21858 were created

Issue Available: @engcom-Delta, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

Same Issue for me Magento 2.3.3 PHP 7.2.26. php module fileinfo is installed and enabled. but still getting error file validation failed.

if i change this return $this->fileMime->getMimeType($this->_file['tmp_name']); to return $this->_file['type']; it says unsupported file format. i am uploading jpg file.

Can any one tell me how to fix this.

Was this page helpful?
0 / 5 - 0 ratings