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



大家好,我正试图将图像上传到上传文件夹中,并在尝试注意到发生了什么时将该路径插入数据库。我不知道上传图像时到底出现了什么问题,它在URL中显示了上传图像的名称。像这个

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>

有人能帮我做什么错误吗。

提前谢谢。

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+文件+上传&rlz=1C1CHBF_enUS777US777&oq=CODEIGNITER+MULTI&aqs=铬.2.69i57j69i60j0l4.2783j0j4&sourceid=chrome&ie=UTF-8

问题就在这里:

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

它正在创建两种不同的表单,一种是通过html标签创建的,另一种是由CI-html表单助手创建的。因此,请移除外部的,然后重试。

参考

最新更新