I want to add a feedback form in frontend, with an image upload field, How to save form data and images in database?

2 Answers 2


First, you should be add input type file at form and also add enctype='multipart/form-data' and

    <!-- -->
    <div class="field image">
        <label class="label" for="image"><span><?php /* @escapeNotVerified */ echo __('Image') ?></span></label>
        <div class="control">
            <input name="image" id="image" title="<?php /* @escapeNotVerified */ echo __('image') ?>"   type="file" />

At controller, you need upload image via


Add below class in __construct functions and execute function of controller:

 namespace [Namespace];

use  Magento\Framework\App\Filesystem\DirectoryList;
class [MyClass]

 protected $uploaderFactory;
 protected $adapterFactory;
 protected $filesystem;

    public function  __construct(
            \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
            \Magento\Framework\Image\AdapterFactory $adapterFactory,
            \Magento\Framework\Filesystem $filesystem
        $this->uploaderFactory = $uploaderFactory;
        $this->adapterFactory = $adapterFactory;
        $this->filesystem = $filesystem;
     public function execute()
            $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'image']);
            $uploaderFactory->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
            $imageAdapter = $this->adapterFactory->create();
            /* start of validated image */
            $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
            $destinationPath = $mediaDirectory->getAbsolutePath('custom_image');
            $result = $uploaderFactory->save($destinationPath);
            if (!$result) {
                throw new LocalizedException(
                    __('File cannot be saved to path: $1', $destinationPath)
            /* you need yo save image 
                 $result['file'] at datbaseQQ 
            $imagepath = $result['file'];
        } catch (\Exception $e) {


$destinationPath give the path till magentoDir/pub/media/custom_image and $result content below

enter image description here

  • How to call this model from controller?
    – Nismath vi
    Sep 18, 2018 at 12:16
  • How to uploads multiple images ?? i have tried with foreach but not succeed Oct 19, 2018 at 6:23
  • @amit I want to do same validation in Magento 2.3, so can you please help me how to deal with exception? because when I'm trying to submit form using ajax call, I'm getting Exception error in controller, and not getting response in ajax call. May 29, 2019 at 10:29

I just have a suggestion for you. We can follow the same logic of product file option. We can see how Magento works with the file.

Here is a sample template:


And take a look at

public function processFileQueue() { ..... }

I think it's a good example for uploading image on frontend.

Your Answer

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

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