在zf2

时间:2016-06-27 05:56:13

标签: php image forms zend-framework2

我正在使用ZF2多个图片上传,这意味着我把文件按钮一个接到一个并尝试上传一个接一个,但它给出了以下错误。请帮我找出错误相同的代码工作正常如果有表格中只有一个文件标签。

  

注意:第989行的C:\ xampp \ htdocs \ ZendFirstProjectNew1 \ module \ Stall \ src \ Stall \ Controller \ StallController.php中的数组到字符串转换

我被困在这里,代码的细节在下面

表格

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

控制器

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

1 个答案:

答案 0 :(得分:0)

如果控制器中的行 989 是此行:

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

你得到这个错误:

  

注意:第989行的C:\ xampp \ htdocs \ ZendFirstProjectNew1 \ module \ Stall \ src \ Stall \ Controller \ StallController.php中的数组到字符串转换

然后该行上的一个变量是数组而不是字符串:

因此$adapter->getDestination()$image1不是字符串类型。您应该调试代码并重构,以便为这两个变量获取字符串。