title: php code to upload multiple images cakephp
body: <p>i am new in cakephp and try to learn.</p>
  
  <p>my table structure is</p>
  
  <p><strong>table name</strong> - drivers</p>
  
  <p>fields - id,name,</p>
  
  <p><strong>table</strong> - drivers_uploads</p>
  
  <p><strong>fields</strong> - id,driver_id,documentname,documentpath</p>
  
  <p>i try to upload multiple files in my table.</p>
  
  <p>My code is working well in folder it uploads multiple images but in table i want to insert all rows</p>
  
  <p>i want to insert multiple file for same id and the id goes in drivers tables and the remaining files insert multiple rows for same id in drivers_upload table.</p>
  
  <p>thanks in advance.</p>
  
  <p><strong>for ex</strong> </p>
  
  <p>I need data to be inserted in below tables format.</p>
  
  <p>**drivers tables ** </p>
  
  <p>id , name</p>
  
  <p>16   lucky</p>
  
  <p><strong>driver_uploads tables</strong></p>
  
  <p>id,driver_id, document_name</p>
  
  <p>1    16       mo.docx  </p>
  
  <p>2    16       ro.docx   </p>
  
  <p><strong>controller code</strong></p>
  
  <pre><code> public function add() {
          $this-&gt;Driver-&gt;create();
          if ($this-&gt;request-&gt;is('post')) {
                  for($i=1;$i&lt;4;$i++)
                  {
                      if(empty($this-&gt;data['Driver']['document'.$i]['name'])){
                          unset($this-&gt;request-&gt;data['Driver']['document'.$i]);
                      }
  
                      if(!empty($this-&gt;data['Driver']['document'.$i]['name']))
                      {
                          $file=$this-&gt;data['Driver']['document'.$i];
                          $ary_ext=array('jpg','jpeg','gif','png','xlsx','docx'); //array of allowed extensions
                          $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
                          if(in_array($ext, $ary_ext))
                          {
                      move_uploaded_file($file['tmp_name'], APP . 'outsidefiles' .DS. time().$file['name']);  
                  //move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/posts/' . mktime().$file['name']);
                              $this-&gt;request-&gt;data['Driver']['document'.$i] = time().$file['name'];
                          }
                      }
  
                  }
  
              if ($this-&gt;Driver-&gt;save($this-&gt;request-&gt;data)) 
              {
                  $this-&gt;Session-&gt;setFlash('Your post has been saved.');
                  $this-&gt;redirect(array('action' =&gt; 'index'));
              }
              else 
              {
                  $this-&gt;Session-&gt;setFlash('Unable to add your post.');
              }
          }
      }
  </code></pre>
  
  <p><strong>view code</strong>
  </p>
  
  <pre><code>&lt;h1&gt;Add Post&lt;/h1&gt;&lt;?php
  echo $this-&gt;Form-&gt;create('Post', array('url' =&gt; array('action' =&gt; 'add'), 'enctype' =&gt; 'multipart/form-data')); 
  echo $this-&gt;Form-&gt;input('title');
  echo $this-&gt;Form-&gt;input('body', array('rows' =&gt; '3'));
  for($i=1; $i&lt;4; $i++)
  {
  ?&gt;
           &lt;div  id="attachment&lt;?php echo $i;?&gt;" &lt;?php if($i !=1) echo "style='display:none;'";?&gt; &gt;
              &lt;div&gt;
                    &lt;?php echo $this-&gt;Form-&gt;input('image'.$i,array('type'=&gt;'file','label' =&gt; false,'div' =&gt; false));?&gt;
              &lt;/div&gt;
              &lt;div  id="attachmentlink&lt;?php echo $i;?&gt;"  &lt;?php if($i==3) echo "style='display:none;'";?&gt;&gt;&lt;a href="javascript:void(0);" onclick="show('attachment&lt;?php echo $i+1;?&gt;'); hide('attachmentlink&lt;?php echo $i;?&gt;');"&gt;Add Another Attachment&lt;/a&gt;&lt;/div&gt;
              &lt;/div&gt;
              &lt;?php } ?&gt;
  &lt;?php 
  echo $this-&gt;Form-&gt;end('Save Post');
  ?&gt;
  &lt;script&gt;
  function show(target){
      document.getElementById(target).style.display = 'block';
  }
  function hide(target){
      document.getElementById(target).style.display = 'none';
  }
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Below is my print_r data</strong> </p>
  
  <pre><code>echo "&lt;pre&gt;";print_r($this-&gt;request-&gt;data); exit();
  
              if ($this-&gt;Driver-&gt;save($this-&gt;request-&gt;data)) 
  
  
  
  Array
  (
      [Driver] =&gt; Array
          (
              [name] =&gt; raj
              [mobile] =&gt; 9960181380
              [email] =&gt; raj@gmail.com
              [licenceno] =&gt; 512203615803
              [address] =&gt; 452,nagpur
              [document1] =&gt; Array
                  (
                      [name] =&gt; findmelogistics_Phase_III_Project Plan v1.2_8.xls
                      [type] =&gt; application/vnd.ms-excel
                      [tmp_name] =&gt; /tmp/phpXGkLvj
                      [error] =&gt; 0
                      [size] =&gt; 29184
                  )
  
              [document2] =&gt; 1461815092FML Phase 3 Specification 2.docx
              [document3] =&gt; 1461815092Phase 3 - SmartData questions 310316.docx
          )
  
  )
  </code></pre>