想要使用Codeigniter将图片添加到我的帖子中

时间:2018-11-26 18:33:02

标签: php codeigniter-3

我尝试将图像添加到我检查过的所有内容和Google中,但是仍然不能满足我要求的其他方法是我的Post_model和Post Controller

    <?php
class Post_m extends MY_Model
{
    protected $_table_name = 'posts';
    protected $_order_by = 'pubdate desc, id desc';
    protected $_timestamps = TRUE;
    public $rules = array(
        'pubdate' => array(
            'field' => 'pubdate',
            'label' => 'Publication date',
            'rules' => 'trim|required|exact_length[10]'
        ),
        'title' => array(
            'field' => 'title',
            'label' => 'Title',
            'rules' => 'trim|required|max_length[100]'
        ),
        'category_id' => array(
            'category_id' => 'Category_id',
            'label' => 'Category ID',
            'rules' => 'trim|required'
        ),
        'slug' => array(
            'field' => 'slug',
            'label' => 'Slug',
            'rules' => 'trim|required|max_length[100]|url_title'
        ),
        'body' => array(
            'field' => 'body',
            'label' => 'Body',
            'rules' => 'trim|required'
        ),
        'post_image' => array(
            'field' => 'post_image',
            'label' => 'Upload Image',
            'rules' => 'trim'
        )
    );

    public function get_new ()
    {
        $post = new stdClass();
        $post->title = '';
        $post->slug = '';
        $post->body = '';
        $post->post_image = '';
        $post->pubdate = date('Y-m-d');
        return $post;
    }

    public function set_published(){
        $this->db->where('pubdate <=', date('Y-m-d'));
    }

    public function get_recent($limit = 3){

        // Fetch a limited number of recent articles
        $limit = (int) $limit;
        $this->set_published();
        $this->db->limit($limit);
        return parent::get();
    }

和我的帖子控制器

<?php

/**
 *   Posts CONTROLLER
 * @property  post_m
 */
class Posts extends Admin_Controller
{


    public function __construct ()
    {
        parent::__construct();
        $this->load->model('post_m');
    }

    public function index ()
    {
        // Fetch all post

        $this->data['title'] = 'Posts | Gatinbox Admin';
        $this->data['site_name'] = 'Gatinbox Admin';
        $this->data['posts'] = $this->post_m->get();

        // Load view
        $this->data['subview'] = 'admin/post/index';
        $this->load->view('admin/layout', $this->data);
    }

    public function edit ($id = NULL)
    {
        // Fetch a post or set a new one
        if ($id) {
            $this->data['post'] = $this->post_m->get($id);
        }
        elseif ($this->data['errors']){
            echo 'post could not be found';
        }
        else {
            $this->data['post'] = $this->post_m->get_new();

        }

        // Set up the form
        $rules = $this->post_m->rules;
        $this->form_validation->set_rules($rules);

        // Process the form
        if ($this->form_validation->run() == TRUE) {
            $data = $this->post_m->array_from_post(array(
                'title',
                'category_id',
                'slug',
                'body',
                'pubdate',
                'post_image'
            ));

            $this->post_m->save($data, $id);
            redirect('admin/posts');
        }

        // Load the view
        $this->data['categories'] = $this->category_m->get_categories();
        $this->data['title'] = 'Edit | Gatinbox Admin';
        $this->data['site_name'] = 'Gatinbox Admin';
        $this->data['subview'] = 'admin/post/edit';
        $this->load->view('admin/layout', $this->data);
    }

    public function delete ($id)
    {
        $this->post_m->delete($id);
        redirect('admin/posts');
    }
}

所以我的问题是 1)如何添加上传配置 2)上传将在post_model还是post控制器中 3)请使用我的代码作为答复,我仍然是codeigniter的新手 预先感谢。

0 个答案:

没有答案