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
JavaScript
x
47
47
1
public function edit()
2
{
3
$data['title'] = 'Edit Profile';
4
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array(); //get username
5
6
$this->form_validation->set_rules('name', 'Full Name', 'required|trim');
7
8
if ($this->form_validation->run() == false) {
9
10
$this->load->view('templates/header', $data);
11
$this->load->view('templates/sidebar', $data);
12
$this->load->view('templates/topbar', $data);
13
$this->load->view('user/edit', $data);
14
$this->load->view('templates/footer');
15
} else {
16
17
$name = $this->input->post('name');
18
$email = $this->input->post('email');
19
20
//cek jika ada gambar yg akan diupload
21
$upload_image = $_FILES['image']['name'];
22
23
if ($upload_image) {
24
$config['allowed_types'] = 'gif|jpg|png';
25
$config['max_size'] = '2048';
26
$config['upload_path'] = './assets/img/profile/';
27
28
$this->load->library('upload', $config);
29
30
if ($this->upload->do_upload('image')) {
31
$new_image = $this->upload->data('file_name');
32
$this->db->set('image', $new_image);
33
} else {
34
echo $this->upload->display_errors();
35
}
36
}
37
38
39
$this->db->set('name', $name);
40
$this->db->where('email', $email);
41
$this->db->update('user');
42
43
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Your profile has been updated</div>');
44
redirect('user');
45
}
46
}
47
My Views
JavaScript
1
39
39
1
<?php echo form_open_multipart('user/edit'); ?>
2
3
<div class="form-group row">
4
<label for="email" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
5
<div class="col-sm-10">
6
<input type="text" class="form-control form-control-sm" id="email" name="email" value="<?php echo $user['email']; ?>" readonly>
7
</div>
8
</div>
9
<div class="form-group row">
10
<label for="name" class="col-sm-2 col-form-label col-form-label-sm">Full Name</label>
11
<div class="col-sm-10">
12
<input type="text" class="form-control form-control-sm" id="name" name="name" value="<?php echo $user['name']; ?>">
13
<?php echo form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
14
</div>
15
</div>
16
<div class="form-group row">
17
<div class="col-sm-2">Picture</div>
18
<div class="col-sm-10">
19
<div class="row">
20
<div class="col-sm-3">
21
<img src="<?php echo base_url('assets/img/profile/') . $user['image']; ?>" class="img-thumbnail">
22
</div>
23
<div class="col-sm-9">
24
<div class="custom-file">
25
<input type="file" class="custom-file-input" id="customFile">
26
<label class="custom-file-label" for="customFile">Choose file</label>
27
</div>
28
</div>
29
</div>
30
</div>
31
</div>
32
33
<div class="form-group row justify-content-end">
34
35
<div class="col-sm-10">
36
<button type="submit" class="btn btn-primary">Save</button>
37
</div>
38
</div>
39
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.
JavaScript
1
39
39
1
<?php echo form_open_multipart('user/edit'); ?>
2
<div class="form-group row">
3
<label for="email" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
4
<div class="col-sm-10">
5
<input type="text" class="form-control form-control-sm" id="email" name="email" value="<?php echo $user['email']; ?>" readonly>
6
</div>
7
</div>
8
<div class="form-group row">
9
<label for="name" class="col-sm-2 col-form-label col-form-label-sm">Full Name</label>
10
<div class="col-sm-10">
11
<input type="text" class="form-control form-control-sm" id="name" name="name" value="<?php echo $user['name']; ?>">
12
<?php echo form_error('name', '<small class="text-danger pl-3">', '</small>'); ?>
13
</div>
14
</div>
15
<div class="form-group row">
16
<div class="col-sm-2">Picture</div>
17
<div class="col-sm-10">
18
<div class="row">
19
<div class="col-sm-3">
20
<img src="<?php echo base_url('assets/img/profile/') . $user['image']; ?>" class="img-thumbnail">
21
</div>
22
<div class="col-sm-9">
23
<div class="custom-file">
24
<input type="file" name="userfile" class="custom-file-input" id="customFile">
25
<label class="custom-file-label" for="customFile">Choose file</label>
26
</div>
27
</div>
28
</div>
29
</div>
30
</div>
31
32
<div class="form-group row justify-content-end">
33
34
<div class="col-sm-10">
35
<button type="submit" class="btn btn-primary">Save</button>
36
</div>
37
</div>
38
</form>
39
And your controller change with bellow..
JavaScript
1
39
39
1
public function edit() {
2
$data['title'] = 'Edit Profile';
3
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array(); //get username
4
5
$this->form_validation->set_rules('name', 'Full Name', 'required|trim');
6
7
if ($this->form_validation->run() == false) {
8
$this->load->view('templates/header', $data);
9
$this->load->view('templates/sidebar', $data);
10
$this->load->view('templates/topbar', $data);
11
$this->load->view('user/edit', $data);
12
$this->load->view('templates/footer');
13
} else {
14
$name = $this->input->post('name');
15
$email = $this->input->post('email');
16
17
$config['allowed_types'] = 'gif|jpg|png';
18
$config['max_size'] = '2048';
19
$config['upload_path'] = './assets/img/profile/';
20
21
$this->load->library('upload', $config);
22
23
if (!$this->upload->do_upload('userfile')) {
24
echo $this->upload->display_errors();
25
} else {
26
$new_image = $this->upload->data('file_name');
27
28
$this->db->set('image', $new_image);
29
$this->db->set('name', $name);
30
$this->db->where('email', $email);
31
$this->db->update('user');
32
33
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Your profile has been updated</div>');
34
35
redirect('user');
36
}
37
}
38
}
39