-1

I have issue with multiple file upload in cakephp.

I try to upload multiple files and need to insert multiple entries in table, but I am unable to do this.

For Ex - if I upload 3 photos from form then need to be inserted 3 rows in table with their file name.

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

                    }

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

add.ctp


    <h1>Add Post</h1><?php
    echo $this->Form->create('Driver', array('url' => array('action' => 'add'), 'enctype' => 'multipart/form-data'));
    echo $this->Form->input('address',array('div' => false, 'class' => 'form-control user-name'));
    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('document'.$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');
?>
6
  • What issue exactly? You need to narrow down the problem and provide more details, do you get an error, etc.? Commented Apr 28, 2016 at 8:55
  • 1
    Same question stackoverflow.com/questions/36888022/… Commented Apr 28, 2016 at 8:56
  • i need suppose i select 3 images from my form then needs to be inserted 3 rows in databse...if i select 2 images then needs to be inserted 2 rows in databse like wise. Commented Apr 28, 2016 at 9:03
  • i need to run the save statement according to the forloop. Commented Apr 28, 2016 at 9:04
  • hi jeroen, i am not getting error,but i am unable to resolve my issue.. Commented Apr 28, 2016 at 9:10

2 Answers 2

0

Plz try this

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','xls','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']); 
                        $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' => 'add'));
        }else{
            $this->Session->setFlash('Unable to add your post.');
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

i have changed in $this->data['Driver']['document'][$i];
0

CakePHP 2 Field naming conventions

echo $this->Form->input('Modelname.0.fieldname');
echo $this->Form->input('Modelname.1.fieldname');

in your view file

//for($i=1; $i<4; $i++)..
echo $this->Form->input('Driver.'.$i.'.document', array('type' => 'file'));

in your controller like

$this->request->data['Driver'][$i]['document']['name']  // where name is uploaded document filename

Use Model::saveMany(array $data = null, array $options = array())

Method used to save multiple rows of the same model at once...

if ($this->Driver->saveMany($this->request->data))...

UPDATE your code

<?php

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

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

        }
        $this->Driver->create();
        if ($this->Driver->saveMany($this->request->data)) 
        {
            //echo "<pre>";print_r($this->request->data); exit();
            $this->Session->setFlash('Your post has been saved.');
            $this->redirect(array('action' => 'index'));
        }
        else 
        {
            $this->Session->setFlash('Unable to add your post.');
        }
    }
}

add.ctp

<h1>Add Post</h1>
<?php
    echo $this->Form->create('Driver', array( 'type' => 'file'));
    //echo $this->Form->input('address',array('div' => false, 'class' => 'form-control user-name')); ????
    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('Driver.'.$i.'.document',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');
?>

3 Comments

no its not working ...plz put complete code and i need all the rows inserted in my table with their image name...i am learner in cake and that why requesting to you to help me to do this if you are a genius..
if you have different logic rather than my code..plz welcome to you to come up with some different code which upload multiple images in uploads folder and multiple rows in table..on same time..
You need two db tables one to store address, other to store related file names, and create relationships like address hasMany documents. In my answer i show you how to create form fields, and how to use data in controller. BUT why not try this plugin github.com/josegonzalez/cakephp-upload/tree/2.x ?

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.