codeigniter上传图像并将路径插入数据库

时间:2018-07-25 07:06:55

标签: mysql codeigniter

嗨,我正在尝试将图像上传到uploads文件夹中,并在尝试进行记录时将该路径插入数据库中。我不知道当我上传图像时它显示的是上传图像名称的确切问题是什么在网址中。像这样

http://localhost/pes/Content/editContent?details=download.jpg&uploadimg=upload

这是我的控制人:

function do_upload() {
    $config['upload_path'] = '/uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['max_width'] = '';
    $config['max_height'] = '';
    $config['overwrite'] = TRUE;
    $config['remove_spaces'] = TRUE;

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

    if (!$this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
        $this->Content_model->insert_images($this->upload->data());
        $this->load->view('template/header');
        $this->load->view('content/content', $error);
        $this->load->view('template/footer');
    } else {
        $this->Content_model->insert_images($this->upload->data());
        $data = array('upload_data' => $this->upload->data());
        $this->load->view('template/header');
        $this->load->view('content/editdescription', $data);
        $this->load->view('template/footer');
    }
}

这是我的模特:

function insert_images($image_data = array()){
  $data = array(
      'details' => $image_data['details']
   );
  $this->db->insert('contentdetails', $data);
 }

这是我的观点:

<form>
                    <?php echo form_open_multipart('Content/do_upload'); ?>
                    <div class="form-group">
                        <div class="col-sm-9">
                            <input type="file" name="details" size="20" multiple="true" />
                        </div>
                    </div>
                    <br /><br />
                    <button type="submit"  name="uploadimg" value="upload" class="form-control btn btn-main">Upload File</button>

                </form>

任何人都可以帮助我犯下什么错误。

谢谢。

2 个答案:

答案 0 :(得分:1)

function do_upload() {
    $config['upload_path'] = './uploads/'; // make folder if doesn't exist; ci won't do this for you
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['max_width'] = '';
    $config['max_height'] = '';
    $config['overwrite'] = TRUE;
    $config['remove_spaces'] = TRUE;

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

    $data['error'] = '';

    if (!$this->upload->do_upload('details')) {
        $data['error'] = $this->upload->display_errors();
        // an error occurred, why would you want to insert nothing?
        //$this->Content_model->insert_images($this->upload->data());
        $this->load->view('template/header');
        $this->load->view('content/content', $data); // echo $error in view
        $this->load->view('template/footer');
    } else {
        $this->Content_model->insert_images($this->upload->data());
        $data['upload_data'] = $this->upload->data();
        $this->load->view('template/header');
        $this->load->view('content/editdescription', $data);
        $this->load->view('template/footer');
    }
}


function insert_images($image_data = array()){
  $data = array(
      'details' => $image_data['full_path']
   );
  $this->db->insert('contentdetails', $data);
 }

HTML:

<input type="file" name="details" size="20" />

删除:

<form>

多个文件:https://www.google.com/search?q=codeigniter+multiple+file+upload&rlz=1C1CHBF_enUS777US777&oq=CODEIGNITER+MULTI&aqs=chrome.2.69i57j69i60j0l4.2783j0j4&sourceid=chrome&ie=UTF-8

答案 1 :(得分:1)

问题在这里:

<form>
<?php echo form_open_multipart('Content/do_upload'); ?>

它正在创建两种不同的形式,一种通过html标签,另一种通过CI html表单帮助器。因此,取下外侧的,然后再试一次。

Reference