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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?