Menu Close
    Hire us Request quote Reading list Switch to dark mode

    how to upload image in magento2

    To upload an image in magento, you need to write following code inside your controller:


    Here,

    _fileUploaderFactory: Is an object of \Magento\MediaStorage\Model\File\UploaderFactory , which handles all the uploading process.

    [‘fileId’ => ‘image’]: Where ‘image’ is name of your input type = file element.

    setAllowedExtensions(): List of file extensions you allowed to upload.

    setFilesDispersion():  If you set it to “false” then your file uploaded to the same path, and if you set it to “true” then it first creates two folders with first two letters of image’s name, and then upload your image inside that folder.

    save(): Is used to upload and save file at the path which you passed inside it.
    If you want to save the image with custom name, then you need to pass your file name as a second parameter.

    . . .

    Comments (3)

    Add Your Comment

  • Bhupendra Jadeja
    $context variable does not have correct path it should be
    MagentoBackendAppActionContext $context
    • hardik
      where put this code ?
    • Birjitsinh Zala
      thanx bhupendra jadeja
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just received your message and our expert will get back to you shortly.
    Send Again
    Close