0

i am new in cakephp and try to learn.

my table structure is

table name - drivers

fields - id,name,

table - drivers_uploads

fields - id,driver_id,documentname,documentpath

i try to upload multiple files in my table.

My code is working well in folder it uploads multiple images but in table i want to insert all rows

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.

thanks in advance.

for ex

I need data to be inserted in below tables format.

**drivers tables **

id , name

16 lucky

driver_uploads tables

id,driver_id, document_name

1 16 mo.docx

2 16 ro.docx

controller code

 public function add() {
        $this->Driver->create();
        if ($this->request->is('post')) {
                for($i=1;$i<4;$i++)
                {
                    if(empty($this->data['Driver']['document'.$i]['name'])){
                        unset($this->request->data['Driver']['document'.$i]);
                    }

                    if(!empty($this->data['Driver']['document'.$i]['name']))
                    {
                        $file=$this->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->request->data['Driver']['document'.$i] = time().$file['name'];
                        }
                    }

                }

            if ($this->Driver->save($this->request->data)) 
            {
                $this->Session->setFlash('Your post has been saved.');
                $this->redirect(array('action' => 'index'));
            }
            else 
            {
                $this->Session->setFlash('Unable to add your post.');
            }
        }
    }

view code

<h1>Add Post</h1><?php
echo $this->Form->create('Post', array('url' => array('action' => 'add'), 'enctype' => 'multipart/form-data')); 
echo $this->Form->input('title');
echo $this->Form->input('body', array('rows' => '3'));
for($i=1; $i<4; $i++)
{
?>
         <div  id="attachment<?php echo $i;?>" <?php if($i !=1) echo "style='display:none;'";?> >
            <div>
                  <?php echo $this->Form->input('image'.$i,array('type'=>'file','label' => false,'div' => false));?>
            </div>
            <div  id="attachmentlink<?php echo $i;?>"  <?php if($i==3) echo "style='display:none;'";?>><a href="javascript:void(0);" onclick="show('attachment<?php echo $i+1;?>'); hide('attachmentlink<?php echo $i;?>');">Add Another Attachment</a></div>
            </div>
            <?php } ?>
<?php 
echo $this->Form->end('Save Post');
?>
<script>
function show(target){
    document.getElementById(target).style.display = 'block';
}
function hide(target){
    document.getElementById(target).style.display = 'none';
}
</script>

Below is my print_r data

echo "<pre>";print_r($this->request->data); exit();

            if ($this->Driver->save($this->request->data)) 



Array
(
    [Driver] => Array
        (
            [name] => raj
            [mobile] => 9960181380
            [email] => [email protected]
            [licenceno] => 512203615803
            [address] => 452,nagpur
            [document1] => Array
                (
                    [name] => findmelogistics_Phase_III_Project Plan v1.2_8.xls
                    [type] => application/vnd.ms-excel
                    [tmp_name] => /tmp/phpXGkLvj
                    [error] => 0
                    [size] => 29184
                )

            [document2] => 1461815092FML Phase 3 Specification 2.docx
            [document3] => 1461815092Phase 3 - SmartData questions 310316.docx
        )

)
5
  • check the output for the <input> fields. you should have ones with [DriverUploads] or you need to do it in the controller as the save just insert to the database. check book.cakephp.org/2.0/en/models/saving-your-data.html you might need instead of save to use saveAssociated Commented Apr 27, 2016 at 11:38
  • suppose i select 3 files and click save then the files uploads in folder are 3 but i need the data is also inserted 3 rows in table...but my code insert only 1 rows in table... Commented Apr 27, 2016 at 11:49
  • Can u update your code with echo "<pre>"; print_r($this->request->data); echo "</pre>"; die; before $this->Driver->save() is executed and post the result. Commented Apr 27, 2016 at 14:02
  • i added my print_r data. my code only insert one rows in table but i need if i upload 3 images then one row with primary id insert in Table1 and remaining 3 rows with primary id of table1 with images insert in table 2. Commented Apr 28, 2016 at 3:49
  • your file tag should be like <?php echo $this->Form->input('image'.$i.'.',array('type'=>'file','label' => false,'div' => false));?> This be an array Commented Apr 28, 2016 at 8:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.