【问题标题】:Codeigniter 3 - upload image and store string inside databaseCodeigniter 3 - 上传图像并将字符串存储在数据库中
【发布时间】:2016-11-28 16:17:02
【问题描述】:

我想在我的新闻模型中上传图片,但是当我尝试创建新闻项目时,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>

我找不到说明如何将图片字符串存储在数据库中的教程,我的表名为news,列名为featured_image

【问题讨论】:

    标签: file codeigniter upload


    【解决方案1】:

    你的模型

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

    【讨论】:

    • 我不明白。我需要更改 $data = array('upload_data' => $this->upload->data()); ?
    • 我已经更新了答案,请查看。它可以帮助你
    【解决方案2】:

    解决了,放上去

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

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 2011-02-16
      • 2016-10-12
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2017-02-15
      • 1970-01-01
      相关资源
      最近更新 更多