如何使用 codeigniter 从多个输入上传文件

How to upload file from multiple inputs with codeigniter

我有三个不同的输入文件,我想从不同的文件夹上传它们。我的以下代码在上传期间工作正常,但它只会转到始终位于 "org_chart" 文件夹中的单个文件夹。

这是我的控制器:

    foreach ($_FILES as $key => $value) {
        if ($key == "updated-org-chart") {
            $config['allowed_types'] = 'pdf';
        $config['upload_path']   = './assets/manpower_requisition/org_chart/';
        $this->load->library('upload', $config);
        $this->upload->do_upload($key);
        }elseif ($key == "job-description") {
            $config['allowed_types'] = 'pdf';
        $config['upload_path']   = './assets/manpower_requisition/jd/';
        $this->load->library('upload', $config);
        $this->upload->do_upload($key);
        }elseif ($key == "bsc") {
            $config['allowed_types'] = 'pdf';
        $config['upload_path']   = './assets/manpower_requisition/bsc/';
        $this->load->library('upload', $config);
        $this->upload->do_upload($key);
        }
    }

您可以使用如下配置:

foreach ($_FILES as $key => $value) {
    switch ($key) {
        case 'updated-org-chart':
            $config['upload_path']   = './assets/manpower_requisition/org_chart/';
            break;

        case 'job-description':
            $config['upload_path']   = './assets/manpower_requisition/jd/';
            break;

        case 'bsc':
            $config['upload_path']   = './assets/manpower_requisition/bsc/';
            break;

        default:
            $config['upload_path']   = './assets/manpower_requisition/org_chart/';
            break;
    }
    $config['allowed_types'] = 'pdf';
    $this->load->library('upload', $config);
    $this->upload->do_upload($key);
}

经过长时间解决这个问题后,我找到了解决我问题的方法。 Codeigniter 不允许使用新参数调用 class 两次,而是必须 "re-initialize" 上传 class。这里有一些文章似乎与问题相同。 Related Articles

$config['allowed_types'] = 'pdf';    
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload($key);