Codeigniter图像和表格提交一起发出

时间:2015-05-26 10:20:35

标签: php codeigniter codeigniter-upload

我正在使用Codeigniter,并为博客条目提供表单和图片上传功能,除非我在表单上按提交时在数据库中创建两个条目。数据发布到一个条目,图像发布到另一个条目。我将图像文件名保存到数据库并对其执行一些图像处理,以便可以使用各个博客帖子调用它。 CRUD功能和图像上传都来自我一直关注的两个不同的教程,我无法让它们一起工作。任何帮助/见解都表示赞赏。

Controller:blog.php

function get_data_from_post() {

    $data['blog_title'] = $this->input->post('blog_title', TRUE);
    $data['blog_date'] = $this->input->post('blog_date', TRUE);
    $data['blog_author'] = $this->input->post('blog_author', TRUE);
    $data['blog_categories'] = $this->input->post('blog_categories', TRUE);
    $data['blog_content'] = $this->input->post('blog_content', TRUE);
    $data['blog_summery'] = $this->input->post('blog_summery', TRUE);
    $data['blog_quote'] = $this->input->post('blog_quote', TRUE);

    return $data;

}
function get_data_from_db($update_id) {
    $blog_query = $this->get_where($update_id);
    foreach ($blog_query->result() as $row) {
        $data['blog_title'] = $row->blog_title;
        $data['blog_date'] = $row->blog_date;
        $data['blog_author'] = $row->blog_author;
        $data['blog_categories'] = $row->blog_categories;
        $data['blog_content'] = $row->blog_content;
        $data['blog_summery'] = $row->blog_summery;
        $data['blog_quote'] = $row->blog_quote;

    }
    if (!isset($data)) {
        $data = "";
    }
    return $data;

}

function create() {

    $update_id = $this->uri->segment(3);
    $submit = $this->input->post('submit', TRUE);

     if ($submit == "Submit") {
        //person has submitted the form
        $data = $this->get_data_from_post();
     } else {

        if (is_numeric($update_id)) {
            $data = $this->get_data_from_db($update_id);
        }
    }

     if (!isset($data)) {
        $data = $this->get_data_from_post();
    }

     $data['update_id'] = $update_id;

    $data['view_file'] = "create";
    $this->load->module('templates');
    $this->templates->admin_homePage_header($data);
}

function submit() {

     $this->load->library('form_validation');

    $this->form_validation->set_rules('blog_title', 'Blog Title',  'required|xss_clean');
    $this->form_validation->set_rules('blog_content', 'Page Content', 'required|xss_clean');

     if ($this->form_validation->run($this) == FALSE) {
         $this->create();
     } else {
        $image_data = $this->do_upload();
        $data = $this->get_data_from_post();

         $update_id = $this->uri->segment(3);
    if (is_numeric($update_id)) {

        $this->_update($update_id, $data);
    } else {

        $this->_insert($data);
    }

         redirect('blog/manage');

    }
}



function do_upload() {

    // Upload image

    $config['upload_path'] = './images/';
    $config['allowed_types'] = 'gif|jpg|png';
    //      $config['max_size'] = '100';
     //     $config['max_width']  = '1024';
    //      $config['max_height']  = '768';

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

    if (!$this->upload->do_upload()) {
         $error = array('error' => $this->upload->display_errors());

         // $this->load->view('upload_form', $error);
    } else {
        $upload_data = $this->upload->data();
        $data_ary = array(

             'blog_image' => $upload_data['file_name'],
        );

        $this->load->database();
        $this->db->insert('blog', $data_ary);

        $data = array('upload_data' => $this->upload->data());

         // Create thumb
        $image_data = $this->upload->data();

        $config['source_image'] = $image_data['full_path'];
        $config['new_image'] = './images/thumbs/';
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 640;
        $config['height'] = 360;

        $this->load->library('image_lib', $config);
         $this->image_lib->resize();
    }

}

Viewfile:create.php

echo form_open_multipart('blog/submit/' . $update_id);
$data = array(
    'name' => 'blog_title',
    'id' => 'blog_title',
    'value' => $blog_title,);
echo form_input($data);
echo form_upload('userfile');
echo form_submit('submit', 'Submit');

0 个答案:

没有答案