I am trying to upload multiple images from frontend with multiple fields.
I can able to save the single image but I would like to save multiple images.
Here is my controller.
class Index extends \Magento\Framework\App\Action\Action
{
protected $_resultPageFactory;
protected $groupFactory;
protected $uploaderFactory;
protected $adapterFactory;
protected $filesystem;
protected $request;
public function __construct(
Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
GroupFactory $groupFactory,
\Magento\Framework\App\Request\Http $request,
\Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
\Magento\Framework\Image\AdapterFactory $adapterFactory,
\Magento\Framework\Filesystem $filesystem
)
{
parent::__construct($context);
$this->_resultPageFactory = $resultPageFactory;
$this->groupFactory = $groupFactory;
$this->request = $request;
$this->uploaderFactory = $uploaderFactory;
$this->adapterFactory = $adapterFactory;
$this->filesystem = $filesystem;
}
public function execute()
{
//echo "<pre>";
/*$groupCode = $this->request->getParam('group_code');
$groupTotalMembers = $this->request->getParam('total_members');
$groupValidity = $this->request->getParam('group_validity');
$groupMembersName = $this->request->getParam('members_name');
$groupIndividualShippingAmount = $this->request->getParam('individual_shopping_amount');
$groupShoppingAmount = $this->request->getParam('group_shopping_amount');
$groupLink = $this->request->getParam('group_link');*/
try{
$uploaderFactory = $this->uploaderFactory->create(['fileId' => 'member_profile_image'],['fileId13' => 'group_image']);
// Not sure about above line !! I've add second param but not seems working.
$uploaderFactory->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
$imageAdapter = $this->adapterFactory->create();
$uploaderFactory->setAllowRenameFiles(true);
$uploaderFactory->setFilesDispersion(true);
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$destinationPath = $mediaDirectory->getAbsolutePath('group_image');
$result = $uploaderFactory->save($destinationPath);
if (!$result) {
throw new LocalizedException(
__('File cannot be saved to path: $1', $destinationPath)
);
}
$imagepath = $result['file'];
//
} catch (\Exception $e) {
}
//print_r($uploader);
//exit;
//$group = $this->groupFactory->create();
//$group->setCode('My Final123')->save();
$resultPage = $this->_resultPageFactory->create();
return $resultPage;
}
}
Thanks in Advance.