I want to upload and save image using observer or model.
How can I do that?

$imageAdapter = $objectManager->get('Magento\Framework\Image\AdapterFactory')->create();

$uploader->addValidateCallback($po_file, $imageAdapter, 'validateUploadFile');

/** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
$mediaDirectory = $objectManager->get('Magento\Framework\Filesystem')->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save($mediaDirectory->getAbsolutePath('customer/senapro'));
$a= $result['file'];

this code not working


Assuming that you are uploading the image from a Form, you can save the image using a controller:

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Backend\App\Action;

class Upload extends \Magento\Framework\App\Action\Action
   protected $_fileUploaderFactory;

public function __construct(
        \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
        Action\Context $context

    ) {

        $this->_fileUploaderFactory = $fileUploaderFactory;

    public function execute(){

        $uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);

        $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);



        $path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)




If you want to reuse the code, you can move this to a model and call it in your controller.

  • thanks for answer but i am using payment form so i cant create controller because its payment form guide me – imtiaz Mar 19 at 11:13
  • So must be must be submitting some form , you can use the code in that action or create a ajax image uploader – Vishwas Bhatnagar Mar 19 at 11:18
  • ajax image uploader? – imtiaz Mar 19 at 11:18
  • Yes as soon as someone selects a image it is uploaded and saved against your quote , I am not sure what is your use case , why are you uploading image against a payment method so i can't suggest a optimal solution but above code works for image upload – Vishwas Bhatnagar Mar 19 at 11:20
  • can you give any tutorial – imtiaz Mar 19 at 11:21

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.