这是我的控制器
function new_post() {
if ($_POST) {
$config = array( array('field' => 'title', 'rules' => 'trim|requird'),
array('field' => 'post', 'rules' => 'trim|required'));
$this -> load -> library('form_validation');
$this -> form_validation -> set_rules($config);
if ($this -> form_validation -> run() == false) {
$data['errors'] = validation_errors();
}
if ($this->input->post('upload')) {
$this->Gallery_model->do_upload();
}
$data = array('title' => $_POST['title'],
'post' => $_POST['post'],
'active' => $_POST['active']);
$this -> post_m -> insert_post($data);
redirect(base_url() . 'posts/index_admin');
} else {
$this -> load -> view('admin/post_new', $data);
}
}
这是模型代码 class Gallery_model扩展了Model {
var $gallery_path;
var $gallery_path_url;
function Gallery_model() {
parent::Model();
$this->gallery_path = realpath(APPPATH . '../images');
$this->gallery_path_url = base_url().'images/';
}
function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
这是观看代码
form_open_multipart('posts/new_post');
form_textarea('title');
form_textarea('post');
form_input('active');
form_upload('userfile');
form_submit('upload', 'Upload');
form_close();
我的桌子现在它上传很好,但只是想将文件名上传到我添加的新colume 名称为{fileurl}。
答案 0 :(得分:0)
使用 do_upload 功能从模型返回图片名称
function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
//get the image name
$fileurl = $image_data['file_name'];
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 150,
'height' => 100
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
return $fileurl;
}
然后你控制器就是这样 -
function new_post() {
if ($_POST) {
$config = array( array('field' => 'title', 'rules' => 'trim|requird'),
array('field' => 'post', 'rules' => 'trim|required'));
$this -> load -> library('form_validation');
$this -> form_validation -> set_rules($config);
if ($this -> form_validation -> run() == false) {
$data['errors'] = validation_errors();
}
if ($this->input->post('upload')) {
$fileurl = $this->Gallery_model->do_upload();
}
$data = array('title' => $_POST['title'],
'post' => $_POST['post'],
'fileurl' => $fileurl,
'active' => $_POST['active']);
$this -> post_m -> insert_post($data);
redirect(base_url() . 'posts/index_admin');
} else {
$this -> load -> view('admin/post_new', $data);
}
}
试试这个希望它有效。