我有一个包含2个上传文件的表单,我想在不同的配置中上传pdf文件,我试试这个。但只能通过第一次配置保存。我正在搜索可能会使用$this->upload->initialize();
这是我的代码:
function do_upload()
{
$siup['upload_path'] = './upload/siup';
$siup['allowed_types'] = 'pdf';
$siup['file_name'] = 'SIUP_'.str_replace(' ','_',ltrim($this->input->post('nama'),'KM '));
$this->upload->initialize($siup);
$this->load->library('upload', $siup);
$keterangan['upload_path'] = './upload/keterangan';
$keterangan['allowed_types'] = 'pdf';
$keterangan['file_name'] = 'Keterangan_'.str_replace(' ','_',ltrim($this->input->post('nama'),'KM '));
$this->upload->initialize($keterangan);
$this->load->library('upload', $keterangan);
if ( ! $this->upload->do_upload('siup'))
{
$data['error'] = $this->upload->display_errors().'on SIUP';
$data['icon'] = 'upload';
$data['title'] = 'Upload';
$data['header_page'] = 'upload';
$data['content'] = 'master_data/employee_form';
$this->template->index($data);
}elseif(! $this->upload->do_upload('keterangan')){
$data['error'] = $this->upload->display_errors().'on Keteragan';
$data['icon'] = 'upload';
$data['title'] = 'Upload';
$data['header_page'] = 'upload';
$data['content'] = 'master_data/employee_form';
$this->template->index($data);
}
else
{
$data['upload_data'] = $this->upload->data();
$data['icon'] = 'upload';
$data['title'] = 'Upload';
$data['header_page'] = 'upload';
$data['content'] = 'master_data/success';
$this->template->index($data);
}
}
答案 0 :(得分:0)
因此,您在do_upload()
函数之前初始化,因此只有最后一次初始化才有效。你需要在程序上重新安排它,以便初始化第一个文件的配置,然后上传第一个文件,然后清除第一个初始化并使用第二个配置,然后上传第二个文件。
这样的事情:
function do_upload() {
$this->load->library('upload');
$siup['upload_path'] = './upload/siup';
$siup['allowed_types'] = 'pdf';
$siup['file_name'] = 'SIUP_' . str_replace(' ', '_', ltrim($this->input->post('nama'), 'KM '));
$this->upload->initialize($siup);
if (!$this->upload->do_upload('siup')) {
$data['error'] = $this->upload->display_errors() . 'on SIUP';
$data['icon'] = 'upload';
$data['title'] = 'Upload';
$data['header_page'] = 'upload';
$data['content'] = 'master_data/employee_form';
$this->template->index($data);
exit;
}
/**
* need to store in var before second initialization
*/
$siup_fileinfo = $this->upload->data(); // 1st file data
$keterangan['upload_path'] = './upload/keterangan';
$keterangan['allowed_types'] = 'pdf';
$keterangan['file_name'] = 'Keterangan_' . str_replace(' ', '_', ltrim($this->input->post('nama'), 'KM '));
$this->upload->initialize($keterangan, true); // use 2nd param to reset
if (!$this->upload->do_upload('keterangan')) {
$data['error'] = $this->upload->display_errors() . 'on Keteragan';
$data['icon'] = 'upload';
$data['title'] = 'Upload';
$data['header_page'] = 'upload';
$data['content'] = 'master_data/employee_form';
$this->template->index($data);
} else {
$keterangen_fileinfo = $this->upload->data(); // 2nd file data
//$data['upload_data'] = $this->upload->data();
$data['icon'] = 'upload';
$data['title'] = 'Upload';
$data['header_page'] = 'upload';
$data['content'] = 'master_data/success';
$this->template->index($data);
}
}
在第二次初始化之前,以及上传第一个文件之后,您需要将上传数据存储在变量中,否则在第二次上传后它将丢失。我没有使用这些变量,但你必须弄清楚如何处理它们。
请注意,以这种方式设置文件名将不起作用。您需要获得扩展并包含它。见文档:
如果设置CodeIgniter,则会将上传的文件重命名为此名称。该 文件名中提供的扩展名也必须是允许的文件类型。 如果未提供 ,则会使用原始file_name。
https://www.codeigniter.com/userguide3/libraries/file_uploading.html#preferences