Search code examples
phpformscodeigniterfile-upload

how to upload different config upload in the same form in codeigniter


i have a form with 2 upload file and I would like to upload pdf file in different config , I've try this one. but only saving by firs config. i was searching may be use $this->upload->initialize(); this is my code :

function do_upload()
    {
        $siup['upload_path']          = './upload/siup';
        $siup['allowed_types']        = 'pdf';
        $siup['file_name']            = 'SIUP_'.str_replace(' ','_',ltrim($this->input->post('nama'),'KM '));
        $this->upload->initialize($siup);
        $this->load->library('upload', $siup);

        $keterangan['upload_path']          = './upload/keterangan';
        $keterangan['allowed_types']        = 'pdf';
        $keterangan['file_name']            = 'Keterangan_'.str_replace(' ','_',ltrim($this->input->post('nama'),'KM '));
        $this->upload->initialize($keterangan);
        $this->load->library('upload', $keterangan);

        if ( ! $this->upload->do_upload('siup'))
        {
            $data['error']          = $this->upload->display_errors().'on SIUP';
            $data['icon']           = 'upload';
            $data['title']          = 'Upload';
            $data['header_page']    = 'upload';
            $data['content']        = 'master_data/employee_form';
            $this->template->index($data);
        }elseif(! $this->upload->do_upload('keterangan')){
            $data['error']          = $this->upload->display_errors().'on Keteragan';
            $data['icon']           = 'upload';
            $data['title']          = 'Upload';
            $data['header_page']    = 'upload';
            $data['content']        = 'master_data/employee_form';
            $this->template->index($data);
        }
        else
        {
            $data['upload_data']    = $this->upload->data();
            $data['icon']           = 'upload';
            $data['title']          = 'Upload';
            $data['header_page']    = 'upload';
            $data['content']        = 'master_data/success';
            $this->template->index($data);
        }
    } 

Solution

  • So you initialize before the do_upload() function thus only the last initialization is valid. You need to rearrange it procedurally so that you initialize the config for the 1st file, then upload 1st the file, then clear the first initialization and use the 2nd config, then upload the 2nd file.

    Something like this:

    function do_upload() {
    
        $this->load->library('upload');
    
        $siup['upload_path'] = './upload/siup';
        $siup['allowed_types'] = 'pdf';
        $siup['file_name'] = 'SIUP_' . str_replace(' ', '_', ltrim($this->input->post('nama'), 'KM '));
        $this->upload->initialize($siup);
    
        if (!$this->upload->do_upload('siup')) {
            $data['error'] = $this->upload->display_errors() . 'on SIUP';
            $data['icon'] = 'upload';
            $data['title'] = 'Upload';
            $data['header_page'] = 'upload';
            $data['content'] = 'master_data/employee_form';
            $this->template->index($data);
            exit;
        }
    
        /**
         * need to store in var before second initialization
         */
        $siup_fileinfo = $this->upload->data(); // 1st file data
    
        $keterangan['upload_path'] = './upload/keterangan';
        $keterangan['allowed_types'] = 'pdf';
        $keterangan['file_name'] = 'Keterangan_' . str_replace(' ', '_', ltrim($this->input->post('nama'), 'KM '));
        $this->upload->initialize($keterangan, true); // use 2nd param to reset
    
        if (!$this->upload->do_upload('keterangan')) {
            $data['error'] = $this->upload->display_errors() . 'on Keteragan';
            $data['icon'] = 'upload';
            $data['title'] = 'Upload';
            $data['header_page'] = 'upload';
            $data['content'] = 'master_data/employee_form';
            $this->template->index($data);
        } else {
    
            $keterangen_fileinfo = $this->upload->data(); // 2nd file data
    
            //$data['upload_data'] = $this->upload->data();
            $data['icon'] = 'upload';
            $data['title'] = 'Upload';
            $data['header_page'] = 'upload';
            $data['content'] = 'master_data/success';
            $this->template->index($data);
        }
    }
    

    Before the 2nd initialization, and after uploading the first file you need to store the upload data in a variable otherwise it will be lost after the second upload. I've left these variables unused, but you will have to figure out what to do with them.

    Further please note that setting your filename this way will not work. You need to get the extension and include it. See docs:

    If set CodeIgniter will rename the uploaded file to this name. The extension provided in the file name must also be an allowed file type. If no extension is provided in the original file_name will be used.

    https://www.codeigniter.com/userguide3/libraries/file_uploading.html#preferences