phtml form be like this
<form class="form"
name="inquiryFrom"
action="productinquiry/productinquiry/save"
id="inq-form"
method="post"
enctype='multipart/form-data'
autocomplete="off"
>
<fieldset class="fieldset">
<div id="file-upload-section" class="field file">
<label class="label" for="inquiry_msg">
<span><?= $block->escapeHtml('Upload File:') ?></span>
</label>
<div class="control">
<input id="attachment-file"
class="attachment-file"
name="attachment[]"
type="file"
multiple="multiple" />
</div>
</div>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<button type="submit"
id="inq-submit"
class="action submit primary "
title="<?= $block->escapeHtml('Submit') ?>" >
<span><?= $block->escapeHtml('Submit') ?></span>
</button>
</div>
</div>
</form>
For Save Image
protected $fileSystem;
protected $uploaderFactory;
public function __construct(
.............................................
\Magento\Framework\Filesystem $fileSystem,
\Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
.............................................
) {
.............................................
$this->fileSystem = $fileSystem;
$this->uploaderFactory = $uploaderFactory;
.............................................
}
public function execute()
{
$data = $this->getRequest()->getPostValue();
// attechmengt save
$filesData = $this->getRequest()->getFiles('attachment');
$length = count($filesData);
$file_store = '';
try {
for ($i = 0; $i < $length; $i++) {
if ($filesData[$i]['error'] == 0) {
$field_name = "attachment[" . $i . "]";
$uploaderFactory = $this->uploaderFactory->create(["fileId" => $field_name]);
$uploaderFactory->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
$uploaderFactory->setAllowRenameFiles(true);
$uploaderFactory->setFilesDispersion(true);
$mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
$destinationPath = $mediaDirectory->getAbsolutePath('tmp/attachment');
$result = $uploaderFactory->save($destinationPath);
if (!$result) {
throw new LocalizedException(
__('File cannot be saved to path: $1', $destinationPath)
);
}
$file_store .= $result['file'] . ",";
}
}
} catch (\Exception $e) {
$this->messageManager->addError(__('Attachment not Uplaoded, Please try Agrain'));
}
$data['attachment'] = $file_store;
echo "<pre>";
print_r($data)
exit();
.....................................
.....................................
}