如何使用Codeigniter中的不同按钮以单一形式上传多个图像和文本文件?

时间:2018-09-07 11:31:26

标签: php codeigniter

我有一个包含两个选择文件按钮的表单,一个按钮用于图像,第二个按钮用于文件上传,但是在其中,我的图像功能正常工作,但是当我上传文档文件时,该按钮无法正常工作。请帮助我

$count = count($_FILES['userfile']['size']);
                            foreach($_FILES as $key=>$value)
                                for($s=0; $s<=$count-1; $s++) {
                                    $_FILES['userfile']['name']=$value['name'][$s];
                                    $_FILES['userfile']['type']    = $value['type'][$s];
                                    $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
                                    $_FILES['userfile']['error']       = $value['error'][$s];
                                    $_FILES['userfile']['size']    = $value['size'][$s];
                                    $config['upload_path'] = './upload/article_img/';
                                    $config['allowed_types'] = 'gif|jpg|png';
                                    $this->load->library('upload', $config);
                                    $this->upload->do_upload();
                                    $data = $this->upload->data();
                                    $name_array[] = $data['file_name'];
                                }
                                $names= implode(',', $name_array);
                                $this->load->database();
                                $db_data = array(
                                    'article_id'=> $is_product_saved,
                                    'image_title'=>json_encode($this->input->post('image_title')),
                                    'file_name'=> $names,
                                    'created_date' => date('Y-m-d H:i:s')
                                );
                                $is_saved_img = $this->site_model->save_data('article_image',$db_data);

    if($_FILES["files"]["name"] != ''){
                                $output = '';
                                $config["upload_path"] = './upload/article_doc/';
                                $config["allowed_types"] = 'txt|pdf|doc';
                                //$this->load->library('upload', $config);
                                $this->upload->initialize($config);
                                for($count = 0; $count<count($_FILES["files"]["name"]); $count++)
                                {
                                    $_FILES["file"]["name"] = $_FILES["files"]["name"][$count];
                                    $_FILES["file"]["type"] = $_FILES["files"]["type"][$count];
                                    $_FILES["file"]["tmp_name"] = $_FILES["files"]["tmp_name"][$count];
                                    $_FILES["file"]["error"] = $_FILES["files"]["error"][$count];
                                    $_FILES["file"]["size"] = $_FILES["files"]["size"][$count];
                                    if($this->upload->do_upload('file'))
                                        {
                                            $data = $this->upload->data();
                                            $output[] =$data["file_name"];
                                        }
                                    }
                                    $name_doc= implode(',', $output);
                                    $db = array(
                                        'doc_name'=> $name_doc,
                                        'article_id'=> $is_product_saved,
                                        'doc_title'=>json_encode($this->input->post('document_title')),
                                        'created_date' => date('Y-m-d H:i:s')
                                    );
                                    $is_saved_img = $this->site_model->save_data('article_document',$db);
                                }

1 个答案:

答案 0 :(得分:0)

}中删除if statement的工作

if($this->upload->do_upload('file'))
{
    $data = $this->upload->data();
    $output[] =$data["file_name"];
}
// } remove this extra bracket because its end `for loop`