如何将图像路径和名称上传到数据库-Codeigniter [英] How to upload image path and name to database - Codeigniter
问题描述
我知道这个问题已经问过几次了,但是我发现的所有其他问题与我想要的都不一样.
I know that this question has been asked several times, but all the other question I found are different from what I want.
我想将文件名和文件路径上传到名为"factoryimages"的表中.
I want to upload the filename and filepath to a table called 'factoryimages'.
做到这一点的最佳方法是什么?
What's the best way of doing this?
我的控制器功能:
function do_upload()
{
$config['upload_path'] = './assets/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->load->view('members/header');
$this->load->view('members/upload_form', $error);
$this->load->view('members/footer');
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('members/header');
$this->load->view('members/upload_success', $data);
$this->load->view('members/footer');
}
}
我在模型中尝试过此方法,但是没有用:
I tried this in my model, but it didn't work:
function insert_images()
{
$insert_data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('bedrijfimages', $insert_data);
}
我的观点:
<?php echo $error;?>
<br/>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" multiple="true" />
<br /><br />
<input type="submit" value="upload" />
</form>
我收到服务器错误,但看不到它是什么.
I get an server error but i can't see what it is.
我的控制器功能:
function do_upload()
{
$config['upload_path'] = './assets/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->upload_model->insert_images($this->upload->data());
$this->load->view('members/header');
$this->load->view('members/upload_form', $error);
$this->load->view('members/footer');
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('members/header');
$this->load->view('members/upload_success', $data);
$this->load->view('members/footer');
}
}
我的模型功能:
function insert_images($data = array())
{
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('bedrijfimages', $data);
}
可能是什么问题?
推荐答案
您必须将此数据作为数组$this->upload->data()
You must pass this data to your insert as array $this->upload->data()
在控制器上,您必须设置完成验证的时间
At your Controller you must set when validation is done
else
{
$this->MODELNAME->insert_images($this->upload->data());
$data = array('upload_data' => $this->upload->data());
$this->load->view('members/header');
$this->load->view('members/upload_success', $data);
$this->load->view('members/footer');
}
在您的模型上:
function insert_images($image_data = array()){
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('bedrijfimages', $data);
}
您可以在CI文档页面上检查此数组中包含哪些信息: Codeigniter上传库
You can check what information is inside this array at CI documentation page: Codeigniter upload library
这篇关于如何将图像路径和名称上传到数据库-Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!