0

I have two fields in admin side form. Logo and brand_product_image. I am using following code to upload file and save in database.

$fields = array('logo','brand_product_image');

                foreach($fields as $field):
                     $uploader = $this->_objectManager->create(
                        'Magento\MediaStorage\Model\File\Uploader', ['fileId' => $field]
                );
                $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
                /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
                $imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();
                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(true);
                /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
                $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
                        ->getDirectoryRead(DirectoryList::MEDIA);
                $result = $uploader->save($mediaDirectory->getAbsolutePath('brand'));
                if ($result['error'] == 0) {
                    $data[$field] = 'brand' . $result['file'];
                }
                endforeach;

When i am saving data m not getting value of brand_product_image. But i if remove logo from $fields array i am getting value of it.

Please provide some solution.

1 Answer 1

0

Try this create uploader instance before foreach not inside loop

 $uploader = $this->_objectManager->create('Magento\MediaStorage\Model\File\Uploader', ['fileId' => $field]);

    foreach($fields as $field):
         //rest of code
    endforeach;
1

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.