致命错误:调用未定义的方法Security :: sanitize_filename()

时间:2016-09-06 05:53:42

标签: php codeigniter

我在codeigniter中收到文件上传的错误。我正在使用MX_Controller进行模块化开发。我尝试了 $ this-> security-> sanitize_filename(),但仍无效。请在下面查看我的代码,了解型号,视图和控制器。

do_upload函数的控制器代码

 function do_upload()
  {
    // $filename = $this->security->sanitize_filename($this->input->post('upload_file'));
    $config['upload_path']   = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = 10000;
    $config['max_width']     = 10024;
    $config['max_height']    = 7608;
    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if(!$this->upload->do_upload('upload_file')) {
        // $error = array('error' => $this->upload->display_errors());
        // $this->load->view('upload_form', $error);
        echo($this->upload->display_errors());
    } else {
        echo($this->upload->data());
        // $data = array('upload_data' => $this->upload->data());
        // $this->load->view('upload_success', $data);
   }
}

视图代码

<div class="portlet-body form">
     <!-- BEGIN FORM-->
     <?php $attributes = array("class"=>"horizontal-form", "id"=>"file_upload_form"); echo form_open_multipart('Doupload/do_upload', $attributes); ?>
      <div class="form-body">
      <!--/row-->
       <div class="row">
         <div class="col-md-6">
           <div class="form-group">
              <input type="hidden" value="<?php echo($user_id);?>" name="user_id" id="user_id">
               <input type="file" class="file" name="upload_file" id="upload_file">
                 <div id="errorBlock" class="help-block"></div>
              </div>
           </div>
         </div>
     </div>
     <div class="form-actions right">
        <button type="button" class="btn default">Cancelar</button>
        <button type="submit" class="btn" id="submit">Submit</button>
    </div>
 </form>
 <!-- END FORM-->

fileSave函数的模型代码

 public function fileSave($dataArray)
    {
        $savefile = $this->db->insert('mod_financial_user_invoice', $dataArray);
        if($savefile)
        {
            echo('success');
        }
        else
        {
            echo('fail');
        }
    }

1 个答案:

答案 0 :(得分:0)

首先需要在控制器函数中加载安全性方法库:

$this->load->library('security');

如果仍然收到错误消息,请确保安全类文件Security.php位于

/system/core/ folder

和一行

$SEC =& load_class('Security', 'core'); 

位于/system/core/CodeIgniter.php文件中。