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

1 Answer 1


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
    Commented Mar 19, 2019 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 Commented Mar 19, 2019 at 11:18
  • ajax image uploader?
    – imtiaz
    Commented Mar 19, 2019 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 Commented Mar 19, 2019 at 11:20
  • can you give any tutorial
    – imtiaz
    Commented Mar 19, 2019 at 11:21

Your Answer

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

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