0

I want to upload multiple file using below code from different Choose File option, and it works for single file upload.

So I want to upload multiple choose file in one click and save their path in database columns name as file_path1, file_path2, file_path3, file_path4.

For now save path of 1st file in file_path1 column and not uploading and saving path of another three choose upload inputs

<form class="upload-form" name="UploadFrom" action="<?php echo $this->getUrl('investment/documents/index'); ?>" id="upload-form" method="post" enctype='multipart/form-data' autocomplete="off">  
<fieldset class="fieldset row">
    <div class="fields col-md-6">
    <div class="field required">
        <label class="label"><span>Upload Document 1</span></label>
        <div class="control">
        <input type="file" name="filesubmission" id="filesubmission" required/>
        <input name="customer_id" id="customer_id" title="Customer ID" class="input-text" type="hidden" value="<?php echo $customerId ?>" data-validate="{required:true}" readonly>                   
        </div>
    </div>
    <div class="field required">
        <label class="label"><span>Upload Document 2</span></label>
        <div class="control">
        <input type="file" name="filesubmission2" id="filesubmission2" required/>
        </div>
    </div>
    <div class="field required">
        <label class="label"><span>Upload Document 3</span></label>
        <div class="control">
        <input type="file" name="filesubmission3" id="filesubmission3" required/>
        </div>
    </div>
    <div class="field required">
        <label class="label"><span>Upload Document 4</span></label>
        <div class="control">
        <input type="file" name="filesubmission4" id="filesubmission4" required/>
        </div>
    </div>
  <div class="actions-toolbar">
    <div class="primary">
        <button type="submit" class="action submit primary" title="Save"><span>Save</span></button>
    </div>
  </div>
  </div>
</fieldset>
</form>

This is controller code

$data = $this->getRequest()->getParams();            
            if ($data) {
                $files = $this->getRequest()->getFiles();
                if (isset($files['filesubmission']) && !empty($files['filesubmission']["name"])){
                    try{
                        $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'filesubmission']);
                        //check upload file type or extension
                        $uploaderFactory->setAllowedExtensions(['pdf']);
                        $fileAdapter = $this->adapterFactory->create();
                        $uploaderFactory->setAllowRenameFiles(true);
                        $uploaderFactory->setFilesDispersion(true);
                        $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
                        $destinationPath = $mediaDirectory->getAbsolutePath('customerdoc/');
                        $result = $uploaderFactory->save($destinationPath);
                        if (!$result) {
                            throw new LocalizedException(
                                __('File cannot be saved to path: $1', $destinationPath)
                            );
                        }
                        $imagePath = 'customerdoc'.$result['file'];
                        $data['file_path1'] = $imagePath;
                        
                        //Store file path and customer id in db
                        $model = $this->documentFactory->create();
                        $model->setData($data)->save();

                        $resultRedirect = $this->resultRedirect->create();
                        $resultRedirect->setPath('investment/documents/index'); 
                        return $resultRedirect;
                        $this->messageManager->addSuccessMessage(__("Data Saved Successfully."));
                    } catch (\Exception $e) {
                        $this->messageManager->addErrorMessage($e->getMessage());
                    }
                }
                
            }
4
  • you have to create multiple instance of $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'filesubmission']); according to your file upload fields Mar 18, 2021 at 4:50
  • Can you please help how it could be done? Mar 18, 2021 at 4:51
  • little busy right now but suggest you to create one function which save the file and return the path. and store that path in one array and save that array in model instead of saving like this. $model = $this->documentFactory->create();$model->setData($data)->save(); Mar 18, 2021 at 5:14
  • Okay thanks for your time I'm trying yet, if possible then add your answer in your mean time Mar 18, 2021 at 5:16

1 Answer 1

1

Try with replacing below code for template and controller

Template

<form class="upload-form" name="UploadFrom" action="<?php echo $this->getUrl('investment/documents/index'); ?>" id="upload-form" method="post" enctype='multipart/form-data' autocomplete="off">
<fieldset class="fieldset row">
  <div class="fields col-md-6">
     <div class="field required">
        <label class="label"><span>Upload Document 1</span></label>
        <div class="control">
           <input type="file" name="filesubmission[]" id="filesubmission" required/>                   
        </div>
     </div>
     <div class="field required">
        <label class="label"><span>Upload Document 2</span></label>
        <div class="control">
           <input type="file" name="filesubmission[]" id="filesubmission2" required/>
        </div>
     </div>
     <div class="field required">
        <label class="label"><span>Upload Document 3</span></label>
        <div class="control">
           <input type="file" name="filesubmission[]" id="filesubmission3" required/>
        </div>
     </div>
     <div class="field required">
        <label class="label"><span>Upload Document 4</span></label>
        <div class="control">
           <input type="file" name="filesubmission[]" id="filesubmission4" required/>
        </div>
     </div>
     <div class="actions-toolbar">
        <div class="primary">
           <button type="submit" class="action submit primary" title="Save"><span>Save</span></button>
        </div>
     </div>
  </div>
</fieldset>
</form>

Controller

$filePathArray = [];
$data = $this->getRequest()->getParams();            
if ($data) {
    $files = $this->getRequest()->getFiles();
    if (isset($files['filesubmission'])){
        
        foreach($files['filesubmission'] as $key => $value){
            if(!empty($value['name'])){
                try{
                    $fileId = "filesubmission[".$key."]";
                    $uploaderFactory = $this->uploaderFactory->create(['fileId' => $fileId]);
                    //check upload file type or extension
                    $uploaderFactory->setAllowedExtensions(['pdf']);
                    $uploaderFactory->setAllowRenameFiles(true);
                    $uploaderFactory->setFilesDispersion(true);
                    $mediaDirectory = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
                    $destinationPath = $mediaDirectory->getAbsolutePath('customerdoc/');
                    $result = $uploaderFactory->save($destinationPath);
                    if (!$result) {
                        throw new LocalizedException(
                            __('File cannot be saved to path: $1', $destinationPath)
                        );
                    }
                    $imagePath = 'customerdoc'.$result['file'];
                    $filePathArray['file_path'.($key + 1)] = $imagePath;
                } catch (\Exception $e) {
                    $this->messageManager->addErrorMessage($e->getMessage());
                }
            }
        }
        //Store file path and customer id in db
        $model = $this->documentFactory->create();
        $model->setData($filePathArray)->save();

        $resultRedirect = $this->resultRedirect->create();
        $resultRedirect->setPath('investment/documents/index'); 
        return $resultRedirect;
        $this->messageManager->addSuccessMessage(__("Data Saved Successfully."));
    }   
}

Thanks!

4
  • Doesn't save file path in columns, thats what I want, have 4 columns in db to save path of files as mentioned in question save paths there Mar 18, 2021 at 9:49
  • @ArunKumar updated my answer, just check values of $filePathArray using var_dump Mar 18, 2021 at 9:59
  • This also not works Mar 18, 2021 at 10:11
  • Did you checked with what values you are getting in $filePathArray ? Mar 18, 2021 at 10:17

Your Answer

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

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