简体   繁体   中英

Uploading images from different inputs CakePHP

I'm trying to upload an image from 2 different inputs on CakePHP. The image is uploaded and saved on a specific folder but when it inserts on the database it only inserts 1. I've been searching but found nothing about.

Controller:

public function Adicionar()
{
    $user = $this->Users->newEntity();
    if ($this->request->is('POST')) 
    {
        $user = $this->Users->patchEntity($user,$this->request->data());

        $dados=$this->request->data('img');
        move_uploaded_file($dados['tmp_name'], WWW_ROOT . '/img/uploaded/' . $dados['name']);
        $user -> img = 'uploaded/' . $dados['name'];
        //die(debug($user -> img = 'uploaded/' . $dados['name']));


        $dados2=$this->request->data('horario');
        move_uploaded_file($dados2['tmp_name'], WWW_ROOT . '/img/Horarios/' . $dados2['name']);
        $user -> img = 'Horarios/' . $dados2['name'];


        $user->pin = (new DefaultPasswordHasher)->hash($user->pin);
        if($this->Users->save($user))
        {
          $this->redirect(['controller'=>'Users','action'=>'listar']);  
        }
    }
    //die(debug($this->set('user',$user)));
    $this->set('user',$user);
}

View:

<?=$this->Form->create($user,array('enctype'=>'multipart/form-data'),['role' => 'form']);?>
        <div class="box-body">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Imagem: </label>
                        <div class="input-group">
                            <?=$this->Form->input('img', ['type' => 'file','class' => 'form-control','label' => '']);?>
                            <div class="input-group-addon">
                                <span class="fa fa-file-image-o"></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label>Horário: </label>
                        <div class="input-group">
                            <?=$this->Form->input('horario',['type' => 'file','class' => 'form-control','label' => '']);?>
                            <div class="input-group-addon">
                                <span class="fa fa-calendar"></span>
                            </div>
                        </div>
                    </div>
                    <!-- /.form group -->
                </div>
            </div>  
        </div>

Users database table

You did not posted your Users db table, but I assume that your problem is due to following:

You are updating img field with:

$user -> img = 'uploaded/' . $dados['name'];

and then you are overwriting it with:

$user -> img = 'Horarios/' . $dados2['name'];

Due to this, you are effectively saving only second image path. Add to your db table another field and save your second image path in new field, or create db table for storing image paths and connect it with users table via user_id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM