Skip to content
Advertisement

uploading image not working in codeigniter 3

i’m new learning codeigniter 3, i have problem that i can’t upload image in edit method. i’m really stuck, i already figure it out from the internet and documentation but there is no answer and my code still not working, when click submit button the method was success update to database but not with the image, here is my code

My Controller

public function edit()
    {
        $data['title'] = 'Edit Profile';
        $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array(); //get username

        $this->form_validation->set_rules('name', 'Full Name', 'required|trim');

        if ($this->form_validation->run() == false) {

            $this->load->view('templates/header', $data);
            $this->load->view('templates/sidebar', $data);
            $this->load->view('templates/topbar', $data);
            $this->load->view('user/edit', $data);
            $this->load->view('templates/footer');
        } else {

            $name = $this->input->post('name');
            $email = $this->input->post('email');

            //cek jika ada gambar yg akan diupload
            $upload_image = $_FILES['image']['name'];

            if ($upload_image) {
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']     = '2048';
                $config['upload_path'] = './assets/img/profile/';

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

                if ($this->upload->do_upload('image')) {
                    $new_image = $this->upload->data('file_name');
                    $this->db->set('image', $new_image);
                } else {
                    echo $this->upload->display_errors();
                }
            }


            $this->db->set('name', $name);
            $this->db->where('email', $email);
            $this->db->update('user');

            $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Your profile has been updated</div>');
            redirect('user');
        }
    }

My Views

            <?php echo form_open_multipart('user/edit'); ?>

            <div class="form-group row">
                <label for="email" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control form-control-sm" id="email" name="email" value="<?php echo $user['email']; ?>" readonly>
                </div>
            </div>
            <div class="form-group row">
                <label for="name" class="col-sm-2 col-form-label col-form-label-sm">Full Name</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control form-control-sm" id="name" name="name" value="<?php echo $user['name']; ?>">
                    <?php echo form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-sm-2">Picture</div>
                <div class="col-sm-10">
                    <div class="row">
                        <div class="col-sm-3">
                            <img src="<?php echo base_url('assets/img/profile/') . $user['image']; ?>" class="img-thumbnail">
                        </div>
                        <div class="col-sm-9">
                            <div class="custom-file">
                                <input type="file" class="custom-file-input" id="customFile">
                                <label class="custom-file-label" for="customFile">Choose file</label>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group row justify-content-end">

                <div class="col-sm-10">
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </div>

Advertisement

Answer

You need to change with your code like this

Change view with name attribute in your file input tag.

<input type="file" class="custom-file-input" id="customFile"> to <input type="file" name="userfile" class="custom-file-input" id="customFile">

and use </form> tag in last.

<?php echo form_open_multipart('user/edit'); ?>
    <div class="form-group row">
        <label for="email" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
        <div class="col-sm-10">
            <input type="text" class="form-control form-control-sm" id="email" name="email" value="<?php echo $user['email']; ?>" readonly>
        </div>
    </div>
    <div class="form-group row">
        <label for="name" class="col-sm-2 col-form-label col-form-label-sm">Full Name</label>
        <div class="col-sm-10">
            <input type="text" class="form-control form-control-sm" id="name" name="name" value="<?php echo $user['name']; ?>">
            <?php echo form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-sm-2">Picture</div>
        <div class="col-sm-10">
            <div class="row">
                <div class="col-sm-3">
                    <img src="<?php echo base_url('assets/img/profile/') . $user['image']; ?>" class="img-thumbnail">
                </div>
                <div class="col-sm-9">
                    <div class="custom-file">
                        <input type="file" name="userfile" class="custom-file-input" id="customFile">
                        <label class="custom-file-label" for="customFile">Choose file</label>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="form-group row justify-content-end">

        <div class="col-sm-10">
            <button type="submit" class="btn btn-primary">Save</button>
        </div>
    </div>
</form>

And your controller change with bellow..

public function edit() {
    $data['title'] = 'Edit Profile';
    $data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array(); //get username

    $this->form_validation->set_rules('name', 'Full Name', 'required|trim');

    if ($this->form_validation->run() == false) {
       $this->load->view('templates/header', $data);
       $this->load->view('templates/sidebar', $data);
       $this->load->view('templates/topbar', $data);
       $this->load->view('user/edit', $data);
       $this->load->view('templates/footer');
    } else {
       $name = $this->input->post('name');
       $email = $this->input->post('email');

       $config['allowed_types'] = 'gif|jpg|png';
       $config['max_size']     = '2048';
       $config['upload_path'] = './assets/img/profile/';
       
       $this->load->library('upload', $config);

       if (!$this->upload->do_upload('userfile')) {
           echo $this->upload->display_errors();
        } else {
           $new_image = $this->upload->data('file_name');
           
           $this->db->set('image', $new_image);
           $this->db->set('name', $name);
           $this->db->where('email', $email);
           $this->db->update('user');

           $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Your profile has been updated</div>');
           
           redirect('user');
        }
    }
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement