【问题标题】:Multiple Image Upload one after another in zf2多张图片在zf2中依次上传
【发布时间】:2016-06-27 05:56:13
【问题描述】:

我正在使用 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);

【问题讨论】:

  • 控制器中的哪一行是989 行?
  • $adapter->addFilter('File\Rename', array('target' => $adapter->getDestination() . DIRECTORY_SEPARATOR .$image1 , 'overwrite' => true));这是第 989 行

标签: php image forms zend-framework2


【解决方案1】:

如果您的控制器中的第 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-&gt;getDestination()$image1 不是字符串类型。您应该调试代码并重构,以便为这两个变量获得一个字符串。

【讨论】:

  • $adapter->getDestination() 是我通过该数组修改的数组,但现在正在上传一张图片,它给出了错误([fileUploadErrorAttack] => File 'php1013.tmp_c-300x229.jpg' 是非法上传。这可能是一次攻击)
  • @BeDeveloper 那是另一个问题。
  • 我从前两天卡住了,现在该怎么办
  • @BeDeveloper 您当前的问题已通过我的回答解决,因此您必须接受答案并继续处理您的新问题。例如 this question here 正在处理同样的问题,但我敢打赌关于同一问题还有更多问题。
猜你喜欢
  • 1970-01-01
  • 2018-10-06
  • 2014-07-14
  • 2016-11-22
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
相关资源
最近更新 更多