php code to upload multiple images cakephp

為{幸葍}努か 提交于 2019-12-12 00:42:34

问题


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] => raj@gmail.com
            [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
        )

)

来源:https://stackoverflow.com/questions/36888022/php-code-to-upload-multiple-images-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!