drfb52000 2018-07-03 10:41
浏览 38
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • dongyulan6251 2018-07-03 10:56
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装