我试图从控制器内部的回调中获取一些额外的信息。
控制器要求验证:
$this->form_validation->set_rules('csvfile', 'Upload CSV', 'callback_csv_upload');
验证回调应该管理文件上传的所有内容。 现在,我想要做的是将上传信息进一步用于控制器:
$this->form_validation->set_rules('csvfile', 'Upload CSV', 'callback_csv_upload');
do something with --- $this->lists->csvinfo;
//控制器内的回调
function csv_upload(){
if($_FILES['csvfile']['size'] != 0){
$upload_dir = './uploads/';
if (!is_dir($upload_dir)) {
mkdir($upload_dir);
}
$config['upload_path'] = $upload_dir;
$config['allowed_types'] = 'csv|txt';
$config['file_name'] = 'csv_'.substr(md5(rand()),0,7);
$config['overwrite'] = false;
$config['max_size'] = '5120';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('csvfile')){
$this->form_validation->set_message('csv_upload', $this->upload->display_errors());
return false;
}
else{
$this->load->model('lists');
$this->lists->set_csvinfo($this->upload->data());
return true;
}
}
else{
$this->form_validation->set_message('csv_upload', "No csv file selected");
return false;
}
}
模型
Class Lists extends CI_Model
{
public $csvinfo = 'xx';
function set_csvinfo($info){
$this->csvinfo = $info;
}
}
就像我在回调中做的那样,不会影响Model变量 - 在打印出默认的'xx'值时。 如果我尝试在控制器中调用de set_csvinfo(),则变量正在改变,但从不在回调函数内调用。
答案 0 :(得分:0)
我认为您的$csvinfo
必须是一种数组,因为$this->upload->data()
会返回数组
Class Lists extends CI_Model
{
public $csvinfo = [];
function set_csvinfo($info){
$this->csvinfo = $info;
}
}
答案 1 :(得分:0)
我的验证回调函数不会触发,除非调用$ this-> form_validation-> run();
function csv_upload(){
.......
$this->lists->csvinfo = 'upload info';
..........
}
$this->form_validation->set_rules('csvfile', 'Upload CSV', 'callback_csv_upload');
$this->form_validation->run();
do something with --- $this->lists->csvinfo;