duai0935 2015-12-31 02:21
浏览 129
已采纳

如何将图像上传到特定文件夹并使用codeigniter重命名

so i have this function in my controller, but i don't know how to implement it in my view, i manage to upload the image into the database but the image that i upload didnt show up in any folder only in database and also i want to rename the image into the date:month:year(timestamp), what do i miss?

Controller

public function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '500';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    if ( ! $this->upload->do_upload('foto'))
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
        $file = $data['upload_data']['full_path'];
    }
}

View

<div class="form-group">
        <label>Foto</label>
        <input type="file" action="<?php echo site_url('admin/barang/do_upload');?>" name="foto" id="foto" class="form-control">
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>

EDIT

so i've been dying to do this stuff, do some research and read a lot of articles but none seems work, as far as i get my view was under form action="admin/barang/insert" , and that makes my 2nd form get ignore, in any way how do we call 2 action in the same form?

  • 写回答

2条回答 默认 最新

  • donglun1918 2016-01-01 20:56
    关注

    the problem was fixed, instead making a new function do_upload() in Controller, i put what inside do_upload() function into my insert() function, so my insert() function have something like this

        //photo
        $photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
        $config['upload_path'] = './assets/img/barang';
        $config['allowed_types'] = 'gif||jpg||png';
        $config['max_size'] = '2048000';
        $config['file_name'] = $photoName;
        $this->load->library('upload',$config);
        if($this->upload->do_upload('userfile')){           
            $upload = 1;
        }
        else{
            $upload = 2;
        }
    

    and then put some if else statement that if upload success, the data will be update otherwise no

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)