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

时间:2015-12-31 02:21:50

标签: php codeigniter image-uploading

所以我在我的控制器中有这个功能,但是我不知道如何在我的视图中实现它,我设法将图像上传到数据库,但我上传的图像只显示在任何文件夹中在数据库中,我也想将图像重命名为日期:月:年(时间戳),我想念什么?

控制器

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'];
    }
}

查看

<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>

修改

所以我一直渴望做这些事情,做一些研究并阅读很多文章,但似乎没有任何工作,只要我的观点是form action="admin/barang/insert",这使我的第二表单被忽略,以任何方式我们如何以相同的形式调用2个动作?

2 个答案:

答案 0 :(得分:0)

试试这个。

在您的观点中:

<?php echo form_open_multipart('admin/barang/do_upload');?>
   <div class="form-group">
      <label>Foto</label>
      <?php echo form_upload('foto'); ?>
   </div>
   <button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
<?php echo form_close(); ?>

在您的控制器上:

public function do_upload()
{
   $config['upload_path'] = './uploads/';//this is the folder where the image is uploaded
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size'] = '500';
   $config['max_width']  = '1024';
   $config['max_height']  = '768';
   $config['file_name'] = 'enter new file name here';//rename file here

   $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'];
   }
}

希望这会对你有所帮助。

答案 1 :(得分:0)

问题已修复,而在控制器中创建新功能do_upload(),我将do_upload()函数内的内容放入我的insert()函数中,所以我的{ {1}}函数有这样的东西

insert()

然后输入一些if else语句,如果上传成功,数据将更新,否则没有