【问题标题】:how to upload image into specific folder and rename it using codeigniter如何将图像上传到特定文件夹并使用 codeigniter 重命名
【发布时间】:2015-12-31 02:21:50
【问题描述】:

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

控制器

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 个动作?

【问题讨论】:

  • 您的上传文件夹是否有写入权限? - 在您的do_upload 成功转储$this-&gt;upload-&gt;data()
  • 是的,它有写权限,do_uploadsuccess dump 是什么意思?
  • 如果do_upload成功则输出$this-&gt;upload-&gt;data(),这样你就可以看到实际存储了哪些数据以及存储在哪里
  • 已经这样做了,但似乎什么也没发生,我认为函数调用正确吗?
  • 是表单动作里面的动作吗?

标签: php codeigniter image-uploading


【解决方案1】:

问题已解决,而是在 Controller 中创建了一个新函数 do_upload(),我将 do_upload() 函数中的内容放入了我的 insert() 函数中,所以我的 insert() 函数有类似的东西这个

    //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;
    }

然后放一些if else语句,如果上传成功,数据将被更新,否则不会

【讨论】:

    【解决方案2】:

    试试这个。

    在你看来:

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

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 2020-08-01
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多