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();
                /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
                $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
                $result = $uploader->save($mediaDirectory->getAbsolutePath('brand'));
                if ($result['error'] == 0) {
                    $data[$field] = 'brand' . $result['file'];

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


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

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.