duanjiagu0655 2019-05-01 09:54
浏览 53
已采纳

如何在zend框架中将多个图像上传到数据库中

I have some code that store multiple image into directory but not inserting into database .please help me out. i am using table gateway. if anyone give me proper and easy way to insert multiple image into database.

this is my controller

StudentController.php

 public function addAction() {
    $form = new StudentForm();
    $request = $this->getRequest();
    if ($request->isPost()) {
        $student = new Student();
        $images=$this->convert_multi_array($request->getFiles()->toArray());
        $post = array_merge_recursive(
        $request->getPost()->toArray(),
        $request->getFiles()->toArray()
    );
          $form->setData($post);
        if ($form->isValid()) {

           $student->exchangeArray($form->getData());
            $this->getStudentTable()->saveStudent($student);
           // return $this->redirect()->toRoute('student');
            print_r($images);
           // print_r($post);
            echo "working";
        }else{
            echo "not working";
        }
    }
    return array('form' => $form,);
}

And this is Model Student.php

  public function getInputFilter() { 

      if (!$this->inputFilter) { 
         $inputFilter = new InputFilter(); 
         $factory     = new InputFactory();
         $inputFilter->add(
                $factory->createInput(array(
                    'name'     => 'image',
                    'required' => true,
                ))
            );
         $inputFilter->add(array( 
            'name' => 'name', 
            'required' => true, 
            'filters' => array( 
               array('name' => 'StripTags'), 
               array('name' => 'StringTrim'), 
            ), 
            'validators' => array( 
               array( 
                  'name' => 'StringLength', 
                  'options' => array( 
                     'encoding' => 'UTF-8', 
                     'min' => 1, 
                     'max' => 100, 
                  ), 
               ), 
            ), 
         )); 
         $inputFilter->add(array( 
            'name' => 'department', 
            'required' => true, 
            'filters' => array( 
               array('name' => 'StripTags'), 
               array('name' => 'StringTrim'), 
            ),  
            'validators' => array( 
               array( 
                  'name' => 'StringLength', 
                  'options' => array( 
                     'encoding' => 'UTF-8', 
                     'min' => 1, 
                     'max' => 100, 
                  ), 
               ), 
            ), 
         )); 
         $inputFilter->add(array( 
            'name' => 'marks', 
            'required' => true, 
            'filters' => array( 
               array('name' => 'StripTags'), 
               array('name' => 'StringTrim'), 
            ),  
            'validators' => array( 
               array( 
                  'name' => 'StringLength', 
                  'options' => array( 
                     'encoding' => 'UTF-8', 
                     'min' => 1, 
                     'max' => 100, 
                  ), 
               ), 
            ), 
         ));  
            $this->inputFilter = $inputFilter; 
      } 
      return $this->inputFilter; 

   }

And

public function exchangeArray($data){
        $this->id = (isset($data['id']))?$data['id']:null;
        //// image////
        if(!empty($data['image'])) { 
         if(is_array($data['image'])) { 
            $this->image = str_replace("./public/tmpuploads/", "", 
               $data['image']['tmp_name']); 
         } else { 
            $this->image = $data['image']; 
         } 
      } else { 
         $data['image'] = null; 
      } 
        $this->name = (isset($data['name']))?$data['name']:null;
        $this->department = (isset($data['department']))?$data['department']:null;
        $this->marks = (isset($data['marks']))?$data['marks']:null;
    }

 public function getArrayCopy() {
        return get_object_vars($this);
    }


my Student Form- StudentForm.php

namespace Student\Form;

use Zend\InputFilter;
use Zend\Form\Form;
use Zend\Form\Element;

class StudentForm extends Form {


         public function __construct($name = null, $options = array())
    {
        parent::__construct($name, $options);
        $this->addElements();
        $this->setInputFilter($this->createInputFilter());
    }
    public function addElements()
    {
        $image = new Element\File('image');

         $image->setLabel('Avatar Image Upload')
             ->setAttribute('id', 'image-file')
             ->setAttribute('multiple', true);  
        $this->add($image);

        $name = new Element\Text('name');
        $name->setLabel('Name');
        $this->add($name);
        $department = new Element\Text('department');
        $department->setLabel('Department');
        $this->add($department);
        $marks = new Element\Text('marks');
        $marks->setLabel('Marks');
        $this->add($marks);

    }
    public function createInputFilter()
    {
        $inputFilter = new InputFilter\InputFilter();
        // File Input
        $image = new InputFilter\FileInput('image');
        $image->setRequired(true);
        $image->getFilterChain()->attachByName(
            'filerenameupload',
            array(
                'target'          => './public/tmpuploads/',
                'overwrite'       => true,
                'use_upload_name' => true,
            )
        );
        $inputFilter->add($image);
        // Text Input
        $name = new InputFilter\Input('name');
        $name->setRequired(true);
        $inputFilter->add($name);
        // Text Input
        $department = new InputFilter\Input('department');
        $department->setRequired(true);
        $inputFilter->add($department);
        // Text Input
        $marks = new InputFilter\Input('marks');
        $marks->setRequired(true);
        $inputFilter->add($marks);


        return $inputFilter;
    }
}

`````
  • 写回答

1条回答 默认 最新

  • doutang1946 2019-05-01 10:22
    关注

    As i understand your code you have to first upload image and then you have to make comma separated string like this

    its only example code

    $imageString = '';
    for($i=0; $i<count($_FILES['file']['name']); $i++){
         $extension = explode('.', basename( $_FILES['file']['name'][$i]));
         $imageName = md5(uniqid()) . "." . $extension[count($extension)-1];
         $path = $path . $imageName; 
    
          if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $path )) {
             $imageString .= $imageName.", ";
          } else{
            echo "Error in Upload";
           }
       }
    $imageName = rtrim($imageName,", ");
    

    // now you can use imageName in insert query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能