使用 PHP 代码点火器上传多个文档


upload multiple documents with PHP Codeigniter

我正在处理用户注册表,其中要求用户上传其个人资料图像和文档以进行验证。这是表单中的两个不同字段。代码工作正常,但问题是它只上传个人资料图像并将其存储在验证文档文件夹中。它不会上传验证文档。如果我删除或评论其中一个字段,它可以正常工作。

下面是我的控制器功能。

    /** UPLOAD PROFILE IMAGE **/
    $prof_pic = $_FILES['profile_pic']['name'];
    $config = array ('upload_path' => './images/tutors/',
                     'allowed_types' => "jpeg|jpg|png",
                     'overwrite' => TRUE
                    );
    $this->load->library('upload', $config);
    $this->upload->do_upload('profile_pic');

    /** UPLOAD VERIFICATION DOCUMENT **/
    $tut_ver_docs = $_FILES['tut_ver_docs']['name'];
    $new_config = array ('upload_path' => './emp_verification_documents/',
                         'allowed_types' => "jpeg|jpg|png",
                         'overwrite' => TRUE
                    );
    $this->load->library('upload', $new_config);
    $this->upload->do_upload('tut_ver_docs');

尝试替换:

$this->load->library('upload', $config);

跟:

$this->load->library('upload');
$this->upload->initialize($config);

替换

$this->load->library('upload', $new_config);

$this->upload->initialize($new_config);

我不完全确定为什么要加载两次上传类:

$this->load->library('upload');
$this->upload->upload_path = './images/tutors/';
$this->upload->allowed_types = array('jpeg','jpg','png');
foreach($_FILES as $name => $file){
    if($name == 'tut_ver_docs'){ //if its the verification document, change the upload path
        $this->upload->upload_path = './emp_verification_documents/';
    }
    if( ! $this->upload->do_upload($name){
        //catch error
    }
}

我没有测试过 但我认为这应该适合您

/** UPLOAD PROFILE IMAGE **/
$this->load->library('upload');//load the library
$config = array ('upload_path' => './images/tutors/',
                 'allowed_types' => "jpeg|jpg|png",
                 'overwrite' => TRUE
                );//config for profile picture
$this->upload->initialize($config);
if (!$this->upload->do_upload('profile_pic'))
{
   print_r($this->upload->display_errors());//upload fails.you can print the error or display somewhere else
}   

/** UPLOAD VERIFICATION DOCUMENT **/
$tut_ver_docs = $_FILES['tut_ver_docs']['name'];
$new_config = array ('upload_path' => './emp_verification_documents/',
                     'allowed_types' => "jpeg|jpg|png",
                     'overwrite' => TRUE
                );
$this->upload->initialize($new_config);
if (!$this->upload->do_upload('tut_ver_docs'))
{
   print_r($this->upload->display_errors());//upload fails
}