douqiang6448 2017-06-23 20:54
浏览 37

从不同输入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

  • 写回答

1条回答 默认 最新

  • duanlei5339 2017-06-24 11:34
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100