简体   繁体   中英

Multiple Image Upload one after another in zf2

I am working ZF2 multiple image upload which means i took to file button one below one and trying to upload that one after another but it gives below error.Please help me to find out the error the same code works fine if there is only one file tag in form.

Notice: Array to string conversion in C:\\xampp\\htdocs\\ZendFirstProjectNew1\\module\\Stall\\src\\Stall\\Controller\\StallController.php on line 989

I am stuck here the details of code are below

Form

namespace Stall\Form;
use Zend\Form\Form;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Adapter;
use Doctrine\ORM\EntityManager;

class MultipleImageForm extends Form
{
    protected $em;

     public function __construct($name = null)
    {
        parent::__construct('stall');
        $this->setAttribute("method","post");
        $this->setAttribute("enctype","multipart/form-data");

        $this->add(array(
            'name' => 'file',
            'attributes' => array(
                'type'  => 'file',
            ),
            'options' => array(
                'label' => 'First Image',
            ),
        )); 

        $this->add(array(
            'name' => 'image',
            'attributes' => array(
                'type'  => 'file',
            ),
            'options' => array(
                'label' => 'Second Image',
            ),
        )); 
          $this->add(array(
           'name' => 'submit',
            'type' => 'submit',
        )); 
    }
}

HTML

$form->setAttribute('action',$this->url('stall',array('action'=>'multipleimage')));
 $form->prepare();

 echo $this->form()->openTag($form);
 echo $this->formRow($form->get('file'));
 echo '<br/>';
 echo $this->formRow($form->get('image'));
 echo $this->formSubmit($form->get('submit'));
 echo $this->form()->closeTag();

Controller

 $form = new MultipleImageForm();
         $form->get('submit')->setValue('Submit');
         $request = $this->getRequest();
         if($request->isPost())
         {
            $nonFile = $request->getPost()->toArray();
            $File    = $this->params()->fromFiles('file');
            $data = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
         //   print_r($data);die;
            //set data post and file ...
            $form->setData($data);
            if ($form->isValid()) 
            { 

                $result = new ImageUpload();
                $image1 = (string) $data['file']['name'];
                $image2 = (string) $data['image']['name'];

                if(!empty($image1))
                {
                    $adapter = new \Zend\File\Transfer\Adapter\Http();
                    $adapter->setDestination('public/img/upload/'); // Returns all known internal file information
                    $adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR .$image1 , 'overwrite' => true));
                    //$adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR."_upload123".$favicon , 'overwrite' => true));
                    if(!$adapter->receive()) 
                    { 
                        $messages = $adapter->getMessages(); 
                    } 
                    else
                    {
                        echo "Image Uploaded";
                    }
                }

                if(!empty($image2))
                {
                    $adapter = new \Zend\File\Transfer\Adapter\Http();
                    $adapter->setDestination('public/img/upload/'); // Returns all known internal file information
                    $adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR .$image2 , 'overwrite' => true));
                    //$adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR."_upload123".$favicon , 'overwrite' => true));
                    if(!$adapter->receive()) 
                    { 
                        $messages = $adapter->getMessages(); 
                    } 
                    else
                    {
                        echo "Image Uploaded";
                    }
                }
            }

         }
         return array('form' => $form);

If line 989 in your controller is this line:

$adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR .$image1 , 'overwrite' => true));

And you get this error:

Notice: Array to string conversion in C:\\xampp\\htdocs\\ZendFirstProjectNew1\\module\\Stall\\src\\Stall\\Controller\\StallController.php on line 989

Then one of the variables on that line is an array instead of a string:

So either $adapter->getDestination() or $image1 is not of type string. You should debug your code and refactor so you get a string for both those variables.

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