如何在 codeigniter 中的同一个控制器 class 中多次调用控制器方法?
How to call a controller method multiple times in the same controller class in codeigniter?
我是 codeigniter 的新手,我在同一个方法中多次调用一个方法时遇到问题 class,这是我的两个控制器方法。
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()
returns 在 upload_file()
中调用时的值相同,这取决于第一次调用,也不允许在 $allowed_audio_types ='*'
上指定文件类型为什么我使用 '*'
,请帮忙,我用谷歌搜索了很多都没有成功。
尝试使用
$this->upload->initialize($config);
在
之后
$this->load->library('upload', $config);
所以你的代码看起来像
$this->load->library('upload', $config);
$this->upload->initialize($config);
这将确保您重置旧的首选项。希望对你有帮助。
我是 codeigniter 的新手,我在同一个方法中多次调用一个方法时遇到问题 class,这是我的两个控制器方法。
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()
returns 在 upload_file()
中调用时的值相同,这取决于第一次调用,也不允许在 $allowed_audio_types ='*'
上指定文件类型为什么我使用 '*'
,请帮忙,我用谷歌搜索了很多都没有成功。
尝试使用
$this->upload->initialize($config);
在
之后$this->load->library('upload', $config);
所以你的代码看起来像
$this->load->library('upload', $config);
$this->upload->initialize($config);
这将确保您重置旧的首选项。希望对你有帮助。