3

I have Created my own Customer Attribute, it was a File attribute. While try to upload a file in that, it shows error like,

"<File Field Name>" is not a valid file.

While checking in Media folder,

There was sub-folders created under customer folder.

But there was no files there.

Example:

If I upload a file like this, abcd.pdf , then it creates folder like this,

/pub/media/customer/a/b/

Can any one help me to fix the issue.?

Here is my Attribute Creation code

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();

    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

     $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();

    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

      $attributeList['my_file'] = [
            'type' => 'varchar',
            'label' => 'My File',
            'input' => 'file',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 150,
            'position' => 150,
            'system' => 0,
            'validate_rules' => serialize(['max_file_size' => 1048576, 'file_extensions' => 'pdf'])
        ]; 
   foreach ($attributeList as $key => $value) {
        $customerSetup->addAttribute(Customer::ENTITY, $key, $value);

        //add attribute to attribute set
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $key)
                        ->addData([
                            'attribute_set_id' => $attributeSetId,
                            'attribute_group_id' => $attributeGroupId,
                            'used_in_forms' => ['customer_account_edit', 'checkout_register', 'adminhtml_customer']
                        ]);

        $attribute->save();
    }
1
  • Show your create attribute code.
    – Sohel Rana
    Jan 9, 2019 at 9:19

2 Answers 2

3

This is a Magento 2 bug. Its already fixed on Magento2.3 develop branch. You need to modify below file:

vendor/magento/module-eav/Model/Attribute/Data/File.php

You need to replace below code:

if (!empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
    return [__('"%1" is not a valid file.', $label)];
}

By

if (empty($value['tmp_name'])) {
    return [__('"%1" is not a valid file.', $label)];
}

Issue link

Github Reference link

1
  • I am getting same issue on Magento 2.3.0 Enterprise Edition. Is this still on this version?
    – konika
    Mar 15, 2019 at 9:09
1

It's almost same like Sukumar mentioned. Although, a bit of change worked for me.

//edit
    if (empty($value['tmp_name']) && !is_uploaded_file($value['tmp_name'])) {
        return [__('"%1" is not a valid file.', $label)];
    }

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.