I need to save below fields in DB. All other fields are saved except Image.But images are saved in pub/media folder.Please suggest me a solution

Here, i have mentioned my controller for save.php


    namespace Namespace\HomeSlider\Controller\Adminhtml\Post;

    class Save extends \Magento\Backend\App\Action

        public function __construct(
        \Magento\Backend\App\Action\Context $context, \Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory
            $this->resultRedirectFactory = $resultRedirectFactory;

        public function execute()
            $id = $this->getRequest()->getParam('id');
            $postModel = $this->_objectManager->create('Namespace\HomeSlider\Model\Post');

            if ($id) {
                $postModel = $postModel->load($id);
            $post = $this->getRequest()->getParams();
      //     echo<pre>;
    //       print_r($post); 
  //         echo</pre>;
     //        exit;
            if(empty($post['id'])) {
                 $post['id'] = null;

            return $this->resultRedirectFactory->create()->setPath('homeslider/post/index');


The parameters are below what i get. But the image is stored as array of array format . How do I overcome this. enter image description here

3 Answers 3


Just add these two line before $postModel->setData($post)

$imageName = $post['image'][0]['file'];
$post['image'] = $imageName;
  • Thank you . I have solved the issue but it is properly working for new action But it is failed to work for edit action in UI form
    – Jaisa
    Commented Nov 15, 2017 at 13:23

In your save action instead of $postModel->setData($post); set data individually. Like below


Do this for all the Column and for image you can do like below


and Then save the model using $postModel->save();

  • I have solved the issue in other way. But it is failed to load exiting image what i was stored in DB while pressing edit
    – Jaisa
    Commented Nov 15, 2017 at 13:28
  • check this answer magento.stackexchange.com/a/201018/20064
    – Piyush
    Commented Nov 15, 2017 at 13:30
  • Am i need to set each colum like this way which you mentioned above ?
    – Jaisa
    Commented Nov 15, 2017 at 13:48
  • Is this a optimized way?
    – Jaisa
    Commented Nov 15, 2017 at 13:48
  • No, it is same as Qaisar's Answer
    – Piyush
    Commented Nov 15, 2017 at 13:52

First,add use Magento\Framework\App\Filesystem\DirectoryList to this class.

Then add below for add code for upload image


 $uploader = $this->_objectManager->create(
    ['fileId' => 'image']
    $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
    /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
    $imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();
    $uploader->addValidateCallback('catalog_product_image', $imageAdapter, 'validateUploadFile');
    /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
    $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
    $result = $uploader->save($mediaDirectory->getAbsolutePath('emizen_banner'));
    if($result['error'] == 0){
    $path = 'emizen_banner' .$result['file'];
    //sucessFully upload
} catch (\Exception $e) {

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.