not able to save 2 files from 1 form in codeigniter

前端 未结 2 1340
死守一世寂寞
死守一世寂寞 2021-01-16 02:30

I have a form that requires a user to upload 2 files, file-upload & imgupload. I am trying to save the entire form through ajax.

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-16 03:00

    Please try this updated function. You were doing some strange things e.g. initializing the first and not the second. As CI is a singleton, this could result in some issues (not saying that this was the root cause).

            $this->load->library('upload');
    
            echo '
    ';
            print_r($_FILES);
    
            $config['upload_path'] = './assets/file/.';
            $config['allowed_types'] = 'gif|jpg|png|doc|txt';
            $config['max_size'] = 1024 * 8;
            $config['encrypt_name'] = TRUE;
            $this->upload->initialize($config);
    
            if (!$this->upload->do_upload('file-upload')) {
                $error = array('error' => $this->upload->display_errors());
                print_r($error);
            } else {
                $data = $this->upload->data();
                echo $res = $data['file_name'];
            }
    
            $config2['upload_path'] = './assets/img/.';
            $config2['allowed_types'] = 'gif|jpg|png|doc|docx|txt';
            $config2['max_size'] = 1024 * 8;
            $config2['encrypt_name'] = TRUE;
    
            $this->upload->initialize($config2, true);
    
            if (!$this->upload->do_upload('imgupload')) {
                $error1 = array('error' => $this->upload->display_errors());
                print_r($error1);
            } else {
                $data1 = $this->upload->data();
                echo $res1 = $data1['file_name'];
            }
    

    If it doesn't work. Please let us know what the $_FILES array is printing.

    UPDATE:

    It wasn't working because $this->load->library('upload', $config2);. When CI sees load it checks first if the class is loaded regardless of the parameters being passed, as it was already loaded this line was ignored and the settings from $config (the first image upload) were being used.

提交回复
热议问题