【问题标题】:error in storing image in upload folder在上传文件夹中存储图像时出错
【发布时间】:2018-07-03 10:41:47
【问题描述】:

我的控制器主页.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>

我正在尝试将图像上传到数据库中并将该图像存储在一个名为上传的文件夹中,下面是我尝试使用的代码,图像名称存储在数据库中但它没有存储在文件夹中,我不知道我哪里出错了请任何人看看我的代码并给我打电话

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

    希望对您有所帮助:

    FCPATH 用于您的$config 中的upload_path

    你的Latest_news 方法应该是这样的:

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

    使用form_open_multipart() 而不是form_open()

    你的表格应该是这样的:

     <?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>
    

    更多:https://www.codeigniter.com/user_guide/libraries/file_uploading.html

    【讨论】:

    • 没有帮助我先生
    • 是的,我收到了错误,因为您没有在其他页面上上传任何图片
    • 你应该使用 form_open_multipart 而不是 form_open 来上传文件,并通过操作查看我的更新答案
    • 我粘贴了我的答案,只是使用它,让我知道你得到了什么
    • 错误号:1048 列“文件名”不能为空插入到latest_newsfirst_contentsecond_contentfilename)值('a','a',NULL)文件名: C:/xampp/htdocs/CodeIgniter_try/system/database/DB_driver.php 行号:691
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多