上传CodeIgniter上更新的文件

时间:2017-01-01 04:23:51

标签: codeigniter-3

大家新年快乐。

我在更新数据的过程中有点困惑。这是我在codeigniter中的控制器

function Ubah($id_pengetahuan) {
    $data['page_title'] = 'Ubah Pengetahuan';

    $ses_id = $this->session->id_pengguna;

    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<div class="alert alert-warning"><strong><i class="fa fa-bullhorn" aria-hidden="true"></i> PESAN : </strong>', '</div>');

    $error_msg = array('required' => '%s yang Anda masukkan belum lengkap.');

    $this->form_validation->set_rules('judul', 'Judul', 'required', $error_msg);
    $this->form_validation->set_rules('konten', 'Konten', 'required', $error_msg);

    if ($id_pengetahuan) {
        $post = $this->mod_pengetahuan->getPengetahuan_id($id_pengetahuan);

        $data['id_pengetahuan'] = $id_pengetahuan;
        $data['id_pengguna'] = $ses_id;
        $data['judul'] = $post->judul;
        $data['konten'] = $post->konten;
        $data['tanggal'] = $post->tanggal;
        $data['url'] = $post->tanggal;
        $data['kategori'] = $post->kategori;
        $data['bagikan'] = $post->bagikan;
        $data['status'] = $post->status;
    }

    if ($this->form_validation->run() == FALSE) {
        $this->view('pengetahuan_edit', $data);  
    }
    else {
        $config['upload_path'] = './assets/img/profil/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '5000'; //kilobyte

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

        if ( !$this->upload->do_upload('url')){                
            $this->session->set_flashdata('save_gagal', 'Tipe File yang Anda Masukkan tidak sesuai.');                
            $this->view('pengetahuan_edit', $data);                
        }
        else{
            $save['id_pengetahuan'] = $id_pengetahuan;
            $save['id_pengguna'] = $ses_id;
            $save['judul'] = $this->input->post('judul');
            $save['konten'] = $this->input->post('konten');
            $save['tanggal'] = date('Y-m-d');
            $save['url'] = $this->upload->data('file_name');
            $save['kategori'] = $this->input->post('kategori');
            $save['bagikan'] = $this->input->post('bagikan');
            $save['status'] = '1';

            $post = $this->mod_pengetahuan->savePengetahuan($save);

            $this->session->set_flashdata('save_succes', 'Pengguna baru berhasil ditambahkan.');

            redirect('pengetahuan');
        }
    }
}

我希望当用户更改数据时,不需要用户上传文件。但是当用户上传文件然后处理下面的代码将运行

if ( !$this->upload->do_upload('url')){                
            $this->session->set_flashdata('save_gagal', 'Tipe File yang Anda Masukkan tidak sesuai.');                
            $this->view('pengetahuan_edit', $data);                
        }

请帮我掌握。谢谢

1 个答案:

答案 0 :(得分:0)

试试这个......

function Ubah($id_pengetahuan) {
    $data['page_title'] = 'Ubah Pengetahuan';

    $ses_id = $this->session->id_pengguna;

    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<div class="alert alert-warning"><strong><i class="fa fa-bullhorn" aria-hidden="true"></i> PESAN : </strong>', '</div>');

    $error_msg = array('required' => '%s yang Anda masukkan belum lengkap.');

    $this->form_validation->set_rules('judul', 'Judul', 'required', $error_msg);
    $this->form_validation->set_rules('konten', 'Konten', 'required', $error_msg);

    if ($id_pengetahuan) {
        $post = $this->mod_pengetahuan->getPengetahuan_id($id_pengetahuan);

        $data['id_pengetahuan'] = $id_pengetahuan;
        $data['id_pengguna'] = $ses_id;
        $data['judul'] = $post->judul;
        $data['konten'] = $post->konten;
        $data['tanggal'] = $post->tanggal;
        $data['url'] = $post->tanggal;
        $data['kategori'] = $post->kategori;
        $data['bagikan'] = $post->bagikan;
        $data['status'] = $post->status;
    }

    if ($this->form_validation->run() == FALSE) {
        $this->view('pengetahuan_edit', $data);  
    }
    else {
        $config['upload_path'] = './assets/img/profil/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '5000'; //kilobyte

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

        if ( !$this->upload->do_upload('url')){                
            $this->session->set_flashdata('save_gagal', 'Tipe File yang Anda Masukkan tidak sesuai.');                
            $this->view('pengetahuan_edit', $data);                
        }
        else{
            $save['id_pengetahuan'] = $id_pengetahuan;
            $save['id_pengguna'] = $ses_id;
            $save['judul'] = $this->input->post('judul');
            $save['konten'] = $this->input->post('konten');
            $save['tanggal'] = date('Y-m-d');
            $save['url'] = $this->upload->data('file_name');
            $save['kategori'] = $this->input->post('kategori');
            $save['bagikan'] = $this->input->post('bagikan');
            $save['status'] = '1';

            $post = $this->mod_pengetahuan->savePengetahuan($save);

            $this->session->set_flashdata('save_succes', 'Pengguna baru berhasil ditambahkan.');

            redirect('pengetahuan');
        }
    }
}