title: How to upload multiple image into database in zend framework
body: <p>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. </p>
  
  <p>this is my controller</p>
  
  <p>StudentController.php</p>
  
  <pre><code> public function addAction() {
      $form = new StudentForm();
      $request = $this-&gt;getRequest();
      if ($request-&gt;isPost()) {
          $student = new Student();
          $images=$this-&gt;convert_multi_array($request-&gt;getFiles()-&gt;toArray());
          $post = array_merge_recursive(
          $request-&gt;getPost()-&gt;toArray(),
          $request-&gt;getFiles()-&gt;toArray()
      );
            $form-&gt;setData($post);
          if ($form-&gt;isValid()) {
  
             $student-&gt;exchangeArray($form-&gt;getData());
              $this-&gt;getStudentTable()-&gt;saveStudent($student);
             // return $this-&gt;redirect()-&gt;toRoute('student');
              print_r($images);
             // print_r($post);
              echo "working";
          }else{
              echo "not working";
          }
      }
      return array('form' =&gt; $form,);
  }
  </code></pre>
  
  <p>And this is Model
  Student.php</p>
  
  <pre><code>  public function getInputFilter() { 
  
        if (!$this-&gt;inputFilter) { 
           $inputFilter = new InputFilter(); 
           $factory     = new InputFactory();
           $inputFilter-&gt;add(
                  $factory-&gt;createInput(array(
                      'name'     =&gt; 'image',
                      'required' =&gt; true,
                  ))
              );
           $inputFilter-&gt;add(array( 
              'name' =&gt; 'name', 
              'required' =&gt; true, 
              'filters' =&gt; array( 
                 array('name' =&gt; 'StripTags'), 
                 array('name' =&gt; 'StringTrim'), 
              ), 
              'validators' =&gt; array( 
                 array( 
                    'name' =&gt; 'StringLength', 
                    'options' =&gt; array( 
                       'encoding' =&gt; 'UTF-8', 
                       'min' =&gt; 1, 
                       'max' =&gt; 100, 
                    ), 
                 ), 
              ), 
           )); 
           $inputFilter-&gt;add(array( 
              'name' =&gt; 'department', 
              'required' =&gt; true, 
              'filters' =&gt; array( 
                 array('name' =&gt; 'StripTags'), 
                 array('name' =&gt; 'StringTrim'), 
              ),  
              'validators' =&gt; array( 
                 array( 
                    'name' =&gt; 'StringLength', 
                    'options' =&gt; array( 
                       'encoding' =&gt; 'UTF-8', 
                       'min' =&gt; 1, 
                       'max' =&gt; 100, 
                    ), 
                 ), 
              ), 
           )); 
           $inputFilter-&gt;add(array( 
              'name' =&gt; 'marks', 
              'required' =&gt; true, 
              'filters' =&gt; array( 
                 array('name' =&gt; 'StripTags'), 
                 array('name' =&gt; 'StringTrim'), 
              ),  
              'validators' =&gt; array( 
                 array( 
                    'name' =&gt; 'StringLength', 
                    'options' =&gt; array( 
                       'encoding' =&gt; 'UTF-8', 
                       'min' =&gt; 1, 
                       'max' =&gt; 100, 
                    ), 
                 ), 
              ), 
           ));  
              $this-&gt;inputFilter = $inputFilter; 
        } 
        return $this-&gt;inputFilter; 
  
     }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>public function exchangeArray($data){
          $this-&gt;id = (isset($data['id']))?$data['id']:null;
          //// image////
          if(!empty($data['image'])) { 
           if(is_array($data['image'])) { 
              $this-&gt;image = str_replace("./public/tmpuploads/", "", 
                 $data['image']['tmp_name']); 
           } else { 
              $this-&gt;image = $data['image']; 
           } 
        } else { 
           $data['image'] = null; 
        } 
          $this-&gt;name = (isset($data['name']))?$data['name']:null;
          $this-&gt;department = (isset($data['department']))?$data['department']:null;
          $this-&gt;marks = (isset($data['marks']))?$data['marks']:null;
      }
  
   public function getArrayCopy() {
          return get_object_vars($this);
      }
  
  
  </code></pre>
  
  <p>my Student Form-
  StudentForm.php</p>
  
  <pre><code>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-&gt;addElements();
          $this-&gt;setInputFilter($this-&gt;createInputFilter());
      }
      public function addElements()
      {
          $image = new Element\File('image');
  
           $image-&gt;setLabel('Avatar Image Upload')
               -&gt;setAttribute('id', 'image-file')
               -&gt;setAttribute('multiple', true);  
          $this-&gt;add($image);
  
          $name = new Element\Text('name');
          $name-&gt;setLabel('Name');
          $this-&gt;add($name);
          $department = new Element\Text('department');
          $department-&gt;setLabel('Department');
          $this-&gt;add($department);
          $marks = new Element\Text('marks');
          $marks-&gt;setLabel('Marks');
          $this-&gt;add($marks);
  
      }
      public function createInputFilter()
      {
          $inputFilter = new InputFilter\InputFilter();
          // File Input
          $image = new InputFilter\FileInput('image');
          $image-&gt;setRequired(true);
          $image-&gt;getFilterChain()-&gt;attachByName(
              'filerenameupload',
              array(
                  'target'          =&gt; './public/tmpuploads/',
                  'overwrite'       =&gt; true,
                  'use_upload_name' =&gt; true,
              )
          );
          $inputFilter-&gt;add($image);
          // Text Input
          $name = new InputFilter\Input('name');
          $name-&gt;setRequired(true);
          $inputFilter-&gt;add($name);
          // Text Input
          $department = new InputFilter\Input('department');
          $department-&gt;setRequired(true);
          $inputFilter-&gt;add($department);
          // Text Input
          $marks = new InputFilter\Input('marks');
          $marks-&gt;setRequired(true);
          $inputFilter-&gt;add($marks);
  
  
          return $inputFilter;
      }
  }
  
  `````
  </code></pre>