1

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

6

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" />
        </div>
    </div>

At controller, you need upload image via

Magento\MediaStorage\Model\File\UploaderFactory

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

 <?php
 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()
    {
        try{
            $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'image']);
            $uploaderFactory->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
            $imageAdapter = $this->adapterFactory->create();
            /* start of validated image */
            $uploaderFactory->addValidateCallback('custom_image_upload',
                $imageAdapter,'validateUploadFile');
            $uploaderFactory->setAllowRenameFiles(true);
            $uploaderFactory->setFilesDispersion(true);
            $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) {
        }
    }
}

Note

$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 '18 at 12:16
  • How to uploads multiple images ?? i have tried with foreach but not succeed – Manish Maheshwari Oct 19 '18 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. – Dhara Bhatti May 29 at 10:29
0

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:

vendor/magento/module-catalog/view/frontend/templates/product/view/options/type/file.phtml

And take a look at

vendor/magento/module-catalog/Model/Product/Type/AbstractType.php
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, privacy policy and cookie policy

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