3

We are using $_FILES to check files post data in core php. Which class can use in Magento 2 to check files post data?

Thanks

2 Answers 2

5

this will help

$files = $this->getRequest()->getFiles();

Here is Magento2 :: Accessing $_FILES using Magento Framework

4
use Magento\MediaStorage\Model\File\UploaderFactory;

protected $uploaderFactory;


public function __construct(
    ...
    UploaderFactory $uploaderFactory    
    ....
) {
    ....
    $this->uploaderFactory = $uploaderFactory;  
    ....
}

    $uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
    $uploader->setFilesDispersion(false);
    $uploader->setFilenamesCaseSensitivity(false);
    $uploader->setAllowRenameFiles(true);
    $path='YOUR DIRECTORY PATH';
    $result = $uploader->save($path);

Where $fileId is your name of input file

3
  • Name of input field is groups[vbanktransfer][nineform] in that case what will be the $fileId ?? <input id="vbanktransfer-nineform" name="groups[vbanktransfer][nineform]" data-ui-id="widget-form-renderer-fieldset-element-0-file-groups-vbanktransfer-nineform" value="" type="file"> Commented Jul 13, 2020 at 16:17
  • It will create the issue (File size is empty)
    – mjdevloper
    Commented Jul 29, 2020 at 10:10
  • can we use this code for multiple file upload fields? Commented Aug 16, 2022 at 10:41

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.