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

My Controller 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'));
            }
        }
    ?>

My model 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>

Im trying to upload a image in database and store that image in one folder called upload, below is the code what i have tried with, the image name is getting stored in the database but its not getting stored in the folder, i don know where im going wrong please can any one look at my code and tel me

Hope this will help you :

Use FCPATH for the upload_path in your $config

Your Latest_news method should be like this :

public function Latest_news()
{  
    $this->form_validation->set_rules('first_content', 'First content', 'required');
    $this->form_validation->set_rules('second_content', 'Second content', 'required');

    if ($this->form_validation->run())
    {
        $config['upload_path']   = FCPATH.'uploads/'; 
        $config['allowed_types'] = 'gif|jpg|png'; 
        $this->load->library('upload', $config);

        if ( $this->upload->do_upload('filename') )
        {
            print_r($this->upload->data());die; 
            $data['first_content'] = $this->input->post('first_content');
            $data['second_content'] = $this->input->post('second_content');
            $data['filename'] = $this->upload->data('file_name');  

            //Transfering data to Model
            $this->Contact_model->latest_news($data);
            //Redirecting to success page
            redirect(site_url('Home/Latest_news'));     
        }
        else
        {
            $error = array('error' => $this->upload->display_errors());
            print_r($error);die;
        }
    }
    else
    {
          $this->load->view('Latest_news'); 

    }
} 

Use form_open_multipart() instead of form_open()

Your form should be like this :

 <?php echo form_open_multipart('Home/Latest_news'); ?>
        <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>

For more : https://www.codeigniter.com/user_guide/libraries/file_uploading.html