I have created a module in magento2 to upload image field. Saving the image hiding the image upload field option in backend

public function execute()
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        $data = $this->getRequest()->getPostValue();
        if($data) {
            if(isset($data['is_active']) && $data['is_active'] === 'true') {
                $data['is_active'] = Notification::STATUS_ENABLED;
                $data['notification_id'] = null;


            if(isset($data['image']) && is_array($data['image'])){
                $data['image'] = null;
            /** @var  \Dzinehub\PushNotification\Model\Notification $notification */
            $model = $this->notificationFactory->create();

            $id = $this->getRequest()->getParam('notification_id');

            if($id) {
                    $model = $this->notificationRepository->getById($id);
                } catch(LocalizedException $e) {
                    $this->messageManager->addErrorMessage(__('Notification no longer exists'));
                    return $resultRedirect->setPath('*/*/');

            /* var_dump($data);

              try {
                $this->messageManager->addSuccessMessage(__('You saved the notification.'));
                if ($this->getRequest()->getParam('back')) {
                    return $resultRedirect->setPath('*/*/edit', ['notification_id' => $model->getId()]);
                return $resultRedirect->setPath('*/*/');
            } catch (LocalizedException $e) {
            } catch (\Exception $e) {
                $this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the notification.'));

            $this->dataPersistor->set('dzpushnotif', $data);
            return $resultRedirect->setPath('*/*/edit', ['notification_id' => $this->getRequest()->getParam('notification_id')]);
        return $resultRedirect->setPath('*/*/');

Also, how to move the image from temporary directory using this function moveFileFromTmp. Any help on this issue?


For Image upload in admin follow the below link

can you check this link for image upload in admin form And also refer this link for save image

|improve this answer|||||

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.