0

How to upload image array in custom from in Magento 2.4

                <div class="file-item-container mb-2">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="field files mb-3">
                                <div class="control">
                                    <label for="files" class="label cw-green-color">
                                        <?= $block->escapeHtmlAttr(__('Upload Images (Max file size: 5mb)')) ?></label>
                                    <input name="files[]" id="files"
                                           value="<?= $block->escapeHtmlAttr($viewModel->getUserName()) ?>"
                                           class="input-text form-control" type="file"/>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
1
  • Please check updated answer and Update me.
    – Msquare
    Nov 19 '20 at 12:00
0

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();

    .....................................
    .....................................
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.