使用codeigniter在同一页面上上传多个文件和单个文件

时间:2017-07-31 16:33:31

标签: php codeigniter codeigniter-2 codeigniter-3

我尝试在一个页面上上传多个文件和个人资料图片,但它无法正常工作。

/** multiple file upload**/
if (!empty($_FILES['attach_file']['name'])) {
    $upload = do_upload($_FILES, $folder);
    if (count($upload) > 0) {
        foreach ($upload as $key => $status) {
            if ($status == 1) {
                $form_data = array(
                    'user_id' => $user_id,
                    'image_name' => $key
                );
                $this->Providerfiles_model->insert($form_data);
            }
        }
    }
}

我正在使用此代码进行多文件上传,但它无法正常工作。它只上传了一份文件。

/***profile pic uplod**/
if ($_FILES && $_FILES['file1']['name'] != "") {
    $folder = FCPATH . 'upload/';
    if ($this->upload->do_upload('file1')) {
        $image_data = $this->upload->data();
        $pic_data = array(
            'user_id' => $user_id,
            'title' => $image_data['file_name']
        );
        $this->profilepic_model->insert($pic_data);
        $image_id = $this->db->insert_id();
    }
}

我正在使用此代码进行个人资料照片上传。它工作正常,但是当我上传多个文档和个人资料图片时,它无法正常工作。文档将保存在配置文件pic表中,我无法找到解决方案。

这是我用于帮助

中的多个文件上传的功能
/** do_upload() in helper**/
function do_upload($files, $folder_name) {
    $return = array();
    $ci = & get_instance();
    $cpt = count($files['attach_file']['name']);
    for ($i = 0; $i < $cpt; $i++) {
        $image = $files['attach_file']['name'][$i];
        $config['file_name'] = $image;
        $_FILES['userfile']['name'] = $image;
        $_FILES['userfile']['type'] = $files['attach_file']['type'][$i];
        $_FILES['userfile']['tmp_name'] = $files['attach_file']['tmp_name'][$i];
        $_FILES['userfile']['error'] = $files['attach_file']['error'][$i];
        $_FILES['userfile']['size'] = $files['attach_file']['size'][$i];
        $ci->upload->initialize($config);

        if ($ci->upload->do_upload()) {
            $data = $ci->upload->data();
            $return[$data['file_name']] = 1;
        } else {
            $return[$image] = 0;
        }
    }

    return $return;
}

0 个答案:

没有答案