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.

Manashvi Birla
8,8739 gold badges29 silver badges53 bronze badges
asked Jul 26, 2017 at 12:45

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;
answered Jul 31, 2017 at 9:20
1
  • i need upload the multiple image in single upload Commented Jan 10, 2020 at 7:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.