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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在zend框架中将多个图像上传到数据库中相关的知识,希望对你有一定的参考价值。

我有一些代码将多个图像存储到目录中但不插入数据库。请帮我解决。我正在使用表网关。如果有人给我正确和简单的方法将多个图像插入数据库。

这是我的控制器

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,);
}

这是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; 

   }

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


我的学生表格 - 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;
    }
}

`````
答案

据我了解你的代码,你必须首先上传图像,然后你必须像这样制作逗号分隔的字符串

它唯一的示例代码

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

//现在您可以在插入查询中使用imageName。

以上是关于如何在zend框架中将多个图像上传到数据库中的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ASP.NET MVC 应用程序中将图像上传到 cloudinary?

在android中将多个图像上传到服务器的最快方法

如何在 django 管理中将多个图像上传与 UPLOADIFY 集成?

zend框架,我可以在其中存储个人资料图片或其他用户上传的图片的路径

在 react-native 中将图像上传到亚马逊 s3

如何在ios中将图像上传到服务器?