duandou8120
duandou8120
2018-09-17 13:53

CodeIgniter 3 - 上传文件错误:未定义属性/在null上调用成员函数

已采纳

I have a problem with codeigniter when I try to send a file. The page returns me the following error:

Error page file upload codeigniter 3

This is my bootstrap modal in file view scadenze.php:

<div class="modal fade" id="uploadFile" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
 aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">

        <?php echo form_open_multipart('corsi/upload_file');?>

        <div class="modal-header modal-blue">
            <h4 class="modal-title" id="myModalLabel">Carica File<br>
                <span id="corso" style="font-size: 14px;color: #888;"></span><span id="utente" style="font-size: 14px;color: #888;"></span>
            </h4>
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        </div>
        <div class="modal-body">

        <input type="file" name="userfile" size="20" />

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Annulla</button>

            <input id="uploadFileSubmit" type="submit" class="btn btn-primary" value="upload">

        </div>
    </form>
    </div>
</div>

And this my controller file Corsi.php:

<?php  
    ob_start();
    defined('BASEPATH') OR exit('No direct script access allowed');  
    class Corsi extends CI_Controller {  
        public function __Construct() {
            parent::__Construct();
            $this->load->helper(array('form', 'url'));
            if(!$this->session->userdata('logged_in')) {
                redirect(base_url());
            }
            $this->load->model('corsi_model');
        }
        //functions  
        function index(){  
            $data["title"] = "Lista Corsi"; 
            $this->load->view('frame/header_view');
            $this->load->view('frame/sidebar_nav_view');
            $this->load->view('corsi/index', $data);  
        }

        public function upload_file() {   
            $this->load->helper(array('form', 'url'));
            $config['upload_path']   = './uploads/attestati/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size']      = 2048000;
            $config['max_width']     = 1024;
            $config['max_height']    = 768;

            $this->load->library('upload', $config);

            // check if folder exists
            if( ! is_dir($config['upload_path'])) {
                @mkdir($config['upload_path'], 0777, true);
            }
            if ( ! $this->corsi->upload_file('userfile')) {
                $this->load->view('frame/header_view');
                $this->load->view('frame/sidebar_nav_view');
                $this->load->view('corsi/creaclasse');
            } else {   
                $this->load->view('frame/header_view');
                $this->load->view('frame/sidebar_nav_view');
                $this->load->view('corsi/creaclasse');
            }
        }
    }

Someone is able to understand where I'm wrong? Thx.. and sorry for my bad English.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dpda53918 dpda53918 3年前

    In your function for upload_file you write:

    $this->corsi->upload_file('userfile')
    

    Which should be:

    $this->upload->do_upload('userfile')
    


    Your error message is telling you that it can't find the property corsi within the instance of $this.

    Please make sure to check CodeIgniters Documentation for their file upload library which can be found here.

    点赞 评论 复制链接分享
  • douling8772 douling8772 3年前

    There is a small mistake in your function calling for 'upload_file', you put the wrong Class name there. It should be upload instead of corsi

    Instead of this wrong calling:-

    $this->corsi->upload_file('userfile')
    

    Use this correct one :-

    $this->upload->do_upload('userfile')
    

    So here is the corrected code for your reference:-

    <?php  
        ob_start();
        defined('BASEPATH') OR exit('No direct script access allowed');  
        class Corsi extends CI_Controller {  
            public function __Construct() {
                parent::__Construct();
                $this->load->helper(array('form', 'url'));
                if(!$this->session->userdata('logged_in')) {
                    redirect(base_url());
                }
                $this->load->model('corsi_model');
            }
            //functions  
            function index(){  
                $data["title"] = "Lista Corsi"; 
                $this->load->view('frame/header_view');
                $this->load->view('frame/sidebar_nav_view');
                $this->load->view('corsi/index', $data);  
            }
    
            public function upload_file() {   
                $this->load->helper(array('form', 'url'));
                $config['upload_path']   = './uploads/attestati/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']      = 2048000;
                $config['max_width']     = 1024;
                $config['max_height']    = 768;
    
                $this->load->library('upload', $config);
    
                // check if folder exists
                if( ! is_dir($config['upload_path'])) {
                    @mkdir($config['upload_path'], 0777, true);
                }
                if ( ! $this->upload->do_upload('userfile')) {
                    $this->load->view('frame/header_view');
                    $this->load->view('frame/sidebar_nav_view');
                    $this->load->view('corsi/creaclasse');
                } else {   
                    $this->load->view('frame/header_view');
                    $this->load->view('frame/sidebar_nav_view');
                    $this->load->view('corsi/creaclasse');
                }
            }
        }
    
    点赞 评论 复制链接分享

相关推荐