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

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

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题