1

这是我的控制器

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

}

这是模型代码类 Gallery_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();

我的表现在上传正常,但只想将文件名上传到我添加的名为 {fileurl} 的新列中。

4

1 回答 1

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

}

试试这个希望它有效。

于 2013-05-26T12:22:15.947 回答