Codeigniter中的文件上传问题

时间:2017-08-07 10:31:36

标签: php codeigniter

我在控制器中有这个错误

  

遇到PHP错误

     

严重性:注意

     

消息:未定义属性:dashboardController :: $ upload

     

文件名:controllers / dashboardController.php

     

行号:70

     

调用非对象中的成员函数data()   C:\ XAMPP \ htdocs中\ High_tack \应用\控制器\ dashboardC ontroller.php   第71行

1 个答案:

答案 0 :(得分:0)

就像我在使用wamp和codeigniter的项目中完成的那样。我将图片直接上传到项目文件夹中名为 uploads 的文件夹中。

这是我的控制器名为 addproduct

<?php class Addproduct extends CI_Controller {
public function __construct() { 
     parent::__construct(); 
     $this->load->helper(array('form', 'url')); 
  }  
public function index(){        
    $this->load->view('addproduct');
}
function save() {
    $name_file = $_FILES['pro_filename']['name'];
    //$user=$this->session->userdata['logged_in'];
    $data=array (
        'pro_name'=>$this->input->post('pro_name'),
        'pro_code'=>$this->input->post('pro_code'),
        'hsn_code'=>$this->input->post('hsn_code'),
        'pro_price'=>$this->input->post('pro_price'),
        'pro_tax1'=>$this->input->post('pro_tax1'),
        'pro_tax2'=>$this->input->post('pro_tax2'),
        'pro_tax3'=>$this->input->post('pro_tax3'),
        'pro_tax4'=>$this->input->post('pro_tax4'),
        'pro_description'=>$this->input->post('pro_description'),
        'pro_brand'=>$this->input->post('pro_brand'),
        'pro_category'=>$this->input->post('pro_category'),
        'pro_scategory'=>$this->input->post('pro_scategory'),
        'pro_sscategory'=>$this->input->post('pro_sscategory'),
        'pro_qauntity'=>$this->input->post('pro_qauntity'),
        'pro_service'=>$this->input->post('pro_service'),
        'pro_certificate'=>$this->input->post('pro_certificate'),
        'pro_filename'=>$name_file,
        'pro_date'=>$this->input->post('pro_date')
        );  
    $this->load->model('base_model');
    $this->base_model->save('addproduct',$data);
    $this->do_upload(); 
    $this->load->view('addproduct');
}
public function do_upload()
{
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png|gif|jpeg';
    $config['max_size']             = 500000;
    $config['max_width']            = 200000;
    $config['max_height']           = 200000;
    $this->load->library('upload', $config);
    $this->upload->do_upload('pro_filename');

}

}

这是我的名为 addproduct

的视图页面
<div class="panel-body">
                        <div class="form">
            <div class="col-md-12 sign-up text-left">
                <div class="form-group ">
                                    <label for="pro_name" class="control-label col-lg-3">Product Name</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_name" name="pro_name" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="pro_code" class="control-label col-lg-3">Product Code</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_code" name="pro_code" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="hsn_code" class="control-label col-lg-3">HSN Code</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="hsn_code" name="hsn_code" type="text" />
                                    </div>
                                </div><br><br>
                 <div class="form-group ">
                                    <label for="pro_price" class="control-label col-lg-3">Prize</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_price" name="pro_price" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="pro_tax1" class="control-label col-lg-3">Tax 1</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_tax1" name="pro_tax1" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="pro_tax2" class="control-label col-lg-3">Tax 2</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_tax2" name="pro_tax2" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="pro_tax3" class="control-label col-lg-3">Tax 3</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_tax3" name="pro_tax3" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="pro_tax4" class="control-label col-lg-3">Tax 4</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_tax4" name="pro_tax4" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="pro_description" class="control-label col-lg-3">Description</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_description" name="pro_description" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="pro_brand" class="control-label col-lg-3">Brand</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_brand" name="pro_brand" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="pro_category" class="control-label col-lg-3">Category</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_category" name="pro_category" type="text" />
                                    </div>
                                </div><br><br>  
                <div class="form-group ">
                                    <label for="pro_scategory" class="control-label col-lg-3">Sub Category</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_scategory" name="pro_scategory" type="text" />
                                    </div>
                                </div><br><br>
                 <div class="form-group ">
                                    <label for="pro_sscategory" class="control-label col-lg-3">S-Sub Category</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_sscategory" name="pro_sscategory" type="text" />
                                    </div>
                                </div><br><br>   
                 <div class="form-group ">
                                    <label for="pro_quantity" class="control-label col-lg-3">Qauntity</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_quantity" name="pro_quantity" type="text" />
                                    </div>
                                </div><br><br>             
                 <div class="form-group ">
                                    <label for="pro_service" class="control-label col-lg-3">Service Alert</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_service" name="pro_service" type="text" />
                                    </div>
                                </div><br><br> 
                <div class="form-group ">
                                    <label for="pro_certificate" class="control-label col-lg-3">Certificate Alert</label>
                                    <div class="col-lg-6">
                                        <input class="form-control " id="pro_certificate" name="pro_certificate" type="text" />
                                    </div>
                                </div><br><br>
                <div class="form-group ">
                                    <label for="pro_filename" class="control-label col-lg-3">Photo:</label>
                                    <div class="col-lg-6">
                                        <input class="form-control" type="file" name="pro_filename" required/>
                                    </div>
                                </div><br><br>
                                <div class="clearfix"></div>
                <?php
                    $today = date("Y-m-d");
                ?>
                <input type="hidden" name="pro_date" value="<?php echo $today ?>">
                <div class="form-group">
                                    <div class="col-lg-offset-3 col-lg-6">
                                        <button class="btn btn-primary" type="submit">Save</button>
                                        <button class="btn btn-default" ><a href="<?php echo base_url();?>addproduct" type="button">Cancel</a></button>
                                    </div>
                                </div>
            <br>
            </div>
            <br>

                        </div>
                    </div>

这是我在名为 base_model

的模型中的代码
    public function save($table, $data)  {
    return $this->db->insert($table, $data);
}

删除你不想要的部分,如果你不需要将图像上传到文件夹,只需更改上传发生的控制器字段

'pro_filename'=>$this->input->post('pro_filename'),