Reputation: 1
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>
Upvotes: 0
Views: 81
Reputation: 1415
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.
Upvotes: 1