简体   繁体   中英

ZF2 Image Upload Warning “Creating default object from empty value”

I am having a issue on image upload in Zend Framework 2, where when i submit the form with image it give a "Creating default object from empty value" warning and file does not save in the folder.

If I submit the form with empty filed for images and upload then the content will save in the DB.

I have add my codes for reference

public function addAction(){
    $form = new Add();
    $brand = new Brand();
    $form->bind($brand);


    $request = $this->getRequest();




    if ($request->isPost()) {

        $post = array_merge_recursive(
            $request->getPost()->toArray(),
            $request->getFiles()->toArray()
        );


        $adapter = new \Zend\File\Transfer\Adapter\Http();
        $files = $adapter->getFileInfo();
        $mediaFileHttpPostName = 'image-file';

        $imageFile = $files[$mediaFileHttpPostName];


        $adapter->setDestination('./public/media');
        $adapter->addValidator('Extension', false, array('jpge'), $mediaFileHttpPostName);

        $adapter->addFilter('Rename',
            array(
                'target'=>  './public/media/'.$imageFile['name'],
                'overwrite'=>true),
            $mediaFileHttpPostName);

        if(!empty($imageFile['name'])){
            if (!$adapter->isValid()){
                $returnObject->errorMessage = $adapter->getMessages();
                $returnObject->result = 0;
            } else {
                try {
                    $adapter->receive($mediaFileHttpPostName);
                    $returnObject->result = 1;
                } catch (\Zend\Filter\Exception\InvalidArgumentException $e) {
                    $returnObject->errorMessage = $e->getMessage();
                    $returnObject->result = 0;
                }
            }
        }

        $form->setData($post );

        if ($form->isValid()) {

             $recordlist = $this->getServiceLocator()->get('BrandService')->insert($brand,'',$form->getHydrator());
            $this->flashMessenger()->addMessage('New brand added!');
           return $this->redirect()->toRoute('zfcadmin/shop/brands');

        }
    }

You are not declaring $returnObject .

You should do:

....
if ($request->isPost()) {
   $returnObject= new stdClass();
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM