Codeigniter 3 - 在数据库中上传图像和存储字符串

时间:2016-11-28 16:17:03

标签: file codeigniter upload

我想在我的新闻模型中上传图片,但当我尝试创建新闻时,CI说:

INSERT INTO `news` (`title`, `slug`, `text`, `featured_image`) VALUES ('sample title', 'sample-title', 'upload image into db', NULL)

我在控制器中的功能是:

public function create()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        // start config image upload
        $config['upload_path'] = './assets/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_width'] = 1024;
        $config['max_height'] = 768;
        $this->load->library('upload', $config);
        // end

        $data['title'] = 'Create a news item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('news/create');
            $this->load->view('templates/footer');

        }
        else
        {

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

            $this->load->view('templates/header');
            $this->load->view('news/success');
            $this->load->view('templates/footer');          
        }
    }

另外,我的模型是:

 public function set_news($id = 0)
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text'),
            'featured_image' => $this->input->post('featured_image')
        );

        if ($id == 0) {
            return $this->db->insert('news', $data);
        } else {
            $this->db->where('id', $id);
            return $this->db->update('news', $data);
        }
    }

我的观点 views / news / create.php

<h2><?php echo $title; ?></h2>
<hr>
<?php echo validation_errors(); ?>

<?php echo form_open_multipart('news/create'); ?>

    <div class="row">
        <div class="six-columns">
            <label for="title">Title</label>
            <input class="u-full-width" type="text" name="title"><br />

            <label for="text">Text</label>
            <textarea class="u-full-width" name="text"></textarea><br />

            <label for="image">Featured image</label>
            <input class="u-full-width" type="file" name="featured_image" /><br />

            <input class="button-primary" type="submit" name="submit" value="Create news item" />
        </div>
    </div>
</form>

我找不到解释如何将图片字符串存储在数据库中的教程,我的表名是新闻,列名是 featured_image

2 个答案:

答案 0 :(得分:0)

你的模特

public function create()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        // start config image upload
        $config['upload_path'] = './assets/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_width'] = 1024;
        $config['max_height'] = 768;
        $this->load->library('upload', $config);
        // end

        $data['title'] = 'Create a news item';

        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('news/create');
            $this->load->view('templates/footer');

        }
        else
        {

            $image_data= $this->upload->data('featured_image');
            $this->load->helper('url');

            $slug = url_title($this->input->post('title'), 'dash', TRUE);

        $data = array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'text' => $this->input->post('text'),
            'featured_image' => $image_data['file_name]
        );
            $this->news_model->set_news(0,$data);

            $this->load->view('templates/header');
            $this->load->view('news/success');
            $this->load->view('templates/footer');          
        }
    }

你的模特

public function set_news($id = 0,$data)
    {
        if ($id == 0) {
            return $this->db->insert('news', $data);
        } else {
            $this->db->where('id', $id);
            return $this->db->update('news', $data);
        }
    }

答案 1 :(得分:0)

解决了,只是把

'featured_image' => $_FILES["featured_image"]["name"]

到我的模型,我在我的控制器中创建了一个回调函数(upload_image)

public function image_upload()
{
    $this->load->library('upload');

    if (!empty($_FILES['featured_image']['name']))
    {
        // Specify configuration for File 1
        $config['upload_path'] = './assets/uploads';
        $config['allowed_types'] = 'gif|jpg|png';

        // Initialize config for File 1 
        $this->upload->initialize($config);

        // Upload file 1
        if ($this->upload->do_upload('featured_image'))
        {
            $data = $this->upload->data('file_name');
            return true;
        }
        else
        {
            $imageerrors = $this->upload->display_errors();
            $this->form_validation->set_message('image_upload', $imageerrors);
            return false;
        } 
    }
}

和我的create()函数

$this->form_validation->set_rules('featured_image', 'Featured image', 'callback_image_upload');