0

我在 Zend Framework 2 中上传图像时遇到问题,当我提交带有图像的表单时,它会给出“从空值创建默认对象”警告,并且文件不会保存在文件夹中。

如果我提交带有空白图像的表单并上传,那么内容将保存在数据库中。

我已添加我的代码以供参考

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');

        }
    }
4

1 回答 1

0

You are not declaring $returnObject.

You should do:

....
if ($request->isPost()) {
   $returnObject= new stdClass();
...
于 2013-10-21T08:35:53.297 回答