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
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
-
How to call this model from controller?Nismath vi– Nismath vi2018-09-18 12:16:53 +00:00Commented Sep 18, 2018 at 12:16
-
How to uploads multiple images ?? i have tried with foreach but not succeedManish Maheshwari– Manish Maheshwari2018-10-19 06:23:26 +00:00Commented 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.Dhara Bhatti– Dhara Bhatti2019-05-29 10:29:42 +00:00Commented 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:
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.
