Note that there are some explanatory texts on larger screens.

plurals
  1. POZF2 Image Upload Warning "Creating default object from empty value"
    text
    copied!<p>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.</p> <p>If I submit the form with empty filed for images and upload then the content will save in the DB.</p> <p>I have add my codes for reference</p> <pre><code>public function addAction(){ $form = new Add(); $brand = new Brand(); $form-&gt;bind($brand); $request = $this-&gt;getRequest(); if ($request-&gt;isPost()) { $post = array_merge_recursive( $request-&gt;getPost()-&gt;toArray(), $request-&gt;getFiles()-&gt;toArray() ); $adapter = new \Zend\File\Transfer\Adapter\Http(); $files = $adapter-&gt;getFileInfo(); $mediaFileHttpPostName = 'image-file'; $imageFile = $files[$mediaFileHttpPostName]; $adapter-&gt;setDestination('./public/media'); $adapter-&gt;addValidator('Extension', false, array('jpge'), $mediaFileHttpPostName); $adapter-&gt;addFilter('Rename', array( 'target'=&gt; './public/media/'.$imageFile['name'], 'overwrite'=&gt;true), $mediaFileHttpPostName); if(!empty($imageFile['name'])){ if (!$adapter-&gt;isValid()){ $returnObject-&gt;errorMessage = $adapter-&gt;getMessages(); $returnObject-&gt;result = 0; } else { try { $adapter-&gt;receive($mediaFileHttpPostName); $returnObject-&gt;result = 1; } catch (\Zend\Filter\Exception\InvalidArgumentException $e) { $returnObject-&gt;errorMessage = $e-&gt;getMessage(); $returnObject-&gt;result = 0; } } } $form-&gt;setData($post ); if ($form-&gt;isValid()) { $recordlist = $this-&gt;getServiceLocator()-&gt;get('BrandService')-&gt;insert($brand,'',$form-&gt;getHydrator()); $this-&gt;flashMessenger()-&gt;addMessage('New brand added!'); return $this-&gt;redirect()-&gt;toRoute('zfcadmin/shop/brands'); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload