尝试将图像存储在上传文件夹中时出错

时间:2018-07-03 18:05:12

标签: php codeigniter codeigniter-3 codeigniter-2

我正在尝试从早上开始工作,但不知道为什么它不起作用,我上传的文件已存储在数据库中,但是当我检查要在其中存储图像的上传文件夹时,图像未保存。请,有人可以告诉我我正在面对什么问题吗?

Home.php

    <?php  
    defined('BASEPATH') OR exit('No direct script access allowed');  

    class Home extends CI_Controller {  

        public function __construct() 
        {
            parent::__construct();

            //load database libray manually
            $this->load->database();

            //load Model
            $this->load->model('Contact_model');

            // load form and url helpers
            $this->load->helper(array('form', 'url'));

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

         public function Latest_news()
        {  
            $this->form_validation->set_rules('first_content', 'First content', 'required');
            $this->form_validation->set_rules('second_content', 'Second content', 'required');
            $config['upload_path']   = './uploads/'; 
            $config['allowed_types'] = 'gif|jpg|png'; 
            $this->load->library('upload', $config);
            if (($this->form_validation->run() == FALSE )&&(!$this->upload->do_upload('filename')))
            {
                $this->load->view('Latest_news'); 
            }
            else
            {
                //insert
                $data = array();
                $data['first_content']=$this->input->post('first_content');
                $data['second_content']=$this->input->post('second_content');
                $data['filename']=$this->input->post('filename');  
                //Transfering data to Model
                $this->Contact_model->latest_news($data);
                //Redirecting to success page
                redirect(site_url('Home/Latest_news'));
            }
        }
    ?>

Contact_model.php

    <?php
        class Contact_model extends CI_Model 
        {
            function latest_news($data)
            {
            //saving records
            $this->db->insert('latest_news', $data); 
            }

        }   
    ?>

Latest_news.php

    <?php echo form_open(); ?>
        <div style="padding-top:10%; padding-left:30%">
            <div>
                <textarea name="first_content" rows="4" cols="50"></textarea>
                <span><?php echo form_error("first_content");?></span>
            </div><br>
            <div>
                <textarea name="second_content" rows="4" cols="50"></textarea>
                <span><?php echo form_error("second_content");?></span>
            </div><br>
            <div>
                <input type="file" name="filename">
                <span><?php echo form_error("filename");?></span>
            </div><br>
          <button type="submit" class="btn btn-default">Submit</button>
        </div>
    </form>

0 个答案:

没有答案