I want to upload Multiple images on frontend side. i have added
enctype and Input type file
the field is accepting multiple images but after printing array of Data its shows only one single image.
Frontend form field code is
<div class="control">
 <input type="file" name="images " id="review_field" data-validate="{required:false}" data-bind="value: review().images" multiple title="Upload Product Images" />
 </div>
when I add name="images[]" and after printing the $Data array, the images field doesn't shows. The screenshot is attached with adding images[] in name. enter image description here
Controller
 /** @var \Magento\Review\Model\Review $review */
 $pathurl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'review_booster/';
 $mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
 $mediapath = $this->_mediaBaseDirectory = rtrim($mediaDir, '/');
 $files = $this->getRequest()->getFiles();
 if (isset($files['image']) && !empty($files['image']["name"])){
 try {
 $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);
 $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
 $uploader->setAllowRenameFiles(true);
 $path = $mediapath . '/review_module/';
 $data['images'] = $files['image']['name'];
 $result = $uploader->save($path);
 }catch (\Exception $e) {
 $this->messageManager->addError(__($e->getMessage()));
 }
 }
- 
 Can you please upload your files code here?Rohan Hapani– Rohan Hapani2018年09月12日 04:18:43 +00:00Commented Sep 12, 2018 at 4:18
- 
 added the complete code.sudo55– sudo552018年09月12日 17:23:19 +00:00Commented Sep 12, 2018 at 17:23
- 
 May this comment help someone later, check this answer: magento.stackexchange.com/a/199132/81049AboElnouR– AboElnouR2021年10月27日 23:38:51 +00:00Commented Oct 27, 2021 at 23:38
2 Answers 2
Try below code:
<div class="control">
 <input type="file" name="images[]" id="review_field" data-validate="{required:false}" 
 data-bind="value: review().images" title="Upload 
 Product Images" multiple/>
</div>
<?php
$pathurl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'review_booster/';
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
$mediapath = $this->_mediaBaseDirectory = rtrim($mediaDir, '/');
//$files = $this->getRequest()->getFiles(); 
$images = $this->getRequest()->getFiles('images');
if(count($files)){
 $i = 0;
 foreach ($images as $files) {
 if (isset($files['tmp_name']) && strlen($files['tmp_name']) > 0) {
 try {
 $uploader = $this->_fileUploaderFactory->create(['fileId' => $images[$i]]);
 $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
 $uploader->setAllowRenameFiles(true);
 $path = $mediapath . '/review_module/';
 $data['images'] = $files['name'];
 $result = $uploader->save($path);
 // echo $result['file']; 
 }catch (\Exception $e) {
 $this->messageManager->addError(__($e->getMessage()));
 }
 }
 $i++;
 }
}
?>
- 
 These answer help to multiple upload imagesuser55776– user557762018年12月21日 10:43:57 +00:00Commented Dec 21, 2018 at 10:43
- 
 i will check this later. Thanks for the helpsudo55– sudo552018年12月21日 19:11:04 +00:00Commented Dec 21, 2018 at 19:11
did you add array on the input image in your form, if you want enable multiple upload image, please try add [] on image input name in the form :
 <input name="image[]" class="image" title="<?php echo __('Image') ?>" />
when you print array of data from the form you should be get multiple image data.
- 
 yes i have added the array.sudo55– sudo552018年12月21日 19:08:56 +00:00Commented Dec 21, 2018 at 19:08
- 
 did you addenctype="multipart/form-datato the form, the form should be to be like this :<form action="..." method=".." enctype="multipart/form-data">mrfizh– mrfizh2018年12月22日 04:46:19 +00:00Commented Dec 22, 2018 at 4:46
- 
 
- 
 hmm... very strange, maybe it's better to show your completely form code. so we could help to analyze more detail.mrfizh– mrfizh2018年12月24日 08:06:03 +00:00Commented Dec 24, 2018 at 8:06
- 
 actually the problem is in the controller where the images will be saved in DB. When i upload one image it is working fine but when I add multiple images it only uploads one image to the database as well as when i print post data array that shows only one image.sudo55– sudo552018年12月24日 19:35:41 +00:00Commented Dec 24, 2018 at 19:35