我是codeigniter的新手,我在同一个类中多次调用一个方法有问题,这是我的两个控制器方法。
Clients.php
public function upload_file(){
$Cover_path = './resources/Cover/';
$Preach_path = './resources/Preaches/';
$Speach_path = './resources/Speaches/';
$Song_path = './resources/Songs/';
$Writting_path = './resources/Writtings/';
$C_allowed_types ='jpg|png|jpeg';
$allowed_audio_types ='*';
$allowed_writtings_types ='pdf|docx|doc';
$C_max_size = '2500000';
$W_max_size = '5000000';
$A_max_size = '6000000';
$width = '0';
$height = '0';
$width1 = '256';
$height1 = '256';
$width2 = '128';
$height2 = '128';
$C_fileName = 'cover_page';
$Other_fileName = 'file';
$action = 'user_uploads';
$file_type = $this->input->post('activity');
if($file_type == 'Writtings'){
$cover_path = $this->do_upload($action,$Cover_path,$C_allowed_types,$C_max_size,$width,$height,$C_fileName);
$file_path = $this->do_upload($action,$Writting_path,$allowed_writtings_types,$W_max_size,$width,$height,$Other_fileName);
}elseif($file_type == 'Song'){
$cover_path = $this->do_upload($action,$Cover_path,$C_allowed_types,$C_max_size,$width1,$height1,$C_fileName);
$file_path = $this->do_upload($action,$Song_path,$allowed_audio_types,$A_max_size,$width,$height,$Other_fileName);
}elseif($file_type == 'Preach'){
$cover_path = $this->do_upload($action,$Cover_path,$C_allowed_types,$C_max_size,$width,$height,$C_fileName);
$file_path = $this->do_upload($action,$Preach_path,$allowed_audio_types,$A_max_size,$width,$height,$Other_fileName);
}elseif($file_type == 'Speach'){
$cover_path = $this->do_upload($action,$Cover_path,$C_allowed_types,$C_max_size,$width2,$height2,$C_fileName);
$file_path = $this->do_upload($action,$Speach_path,$allowed_audio_types,$A_max_size,$width,$height,$Other_fileName);
}
$upload_info = array(
'user_email' => $this->session->userdata['email'],
'file_name' =>$this->input->post('file_name'),
'cover_direc' =>$cover_path,
'file_direc' =>$file_path,
'file_type'=>$file_type,
'date_released' =>$this->input->post('date_released')
);
//var_dump($upload_info['file_direc']); die();
$this->files->upload_file($upload_info);
$this->session->set_flashdata('success_msg', 'Data uploaded successful.');
//$this->session->set_userdata('ext',$upload_info);
redirect('Clients/user_panel');
}
public function do_upload($action,$path,$allowed_types,$max_size,$width,$height,$fileName)
{
$config['upload_path'] = $path;
$config['allowed_types'] = $allowed_types;
$config['max_size'] = $max_size;
$config['max_width'] = $width;
$config['max_height'] = $height;
//redirect('Clients/user_panel');
//var_dump($config['upload_path']); die();
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($fileName))
{
$data = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', $data);
//redirect('Clients/user_panel');
if($action == 'register'){
redirect('Clients/register_view');
}elseif($action == 'user_uploads'){
redirect('Clients/user_panel');
}
}
else
{
$data = $this->upload->data();
return $data['full_path'];
}
}
问题是do_upload()
在upload_file()
中调用时返回相同的值,具体取决于第一次调用,也不允许在$allowed_audio_types ='*'
上指定文件类型,这就是我使用'*'
的原因$(...)
1}},请帮助,我搜索了很多但没有成功。
答案 0 :(得分:2)
尝试使用
$this->upload->initialize($config);
之后
$this->load->library('upload', $config);
所以你的代码看起来像
$this->load->library('upload', $config);
$this->upload->initialize($config);
这将确保您重置旧的首选项。希望它有所帮助。