VIEW:
<form method='post' action="<?php echo base_url('Reg_form/save')?>" enctype="multipart/form-data" name="reg_form">
<div class="form-row">
<div class="form-group col-md-3">
<label for="">First name</label>
<input type="text" class="form-control" id="fname" placeholder="First Name" name='Fname' required="">
</div>
<div class="form-group col-md-3">
<label for="">Last name</label>
<input type="text" class="form-control" id="lname" placeholder="Last Name" name='Lname' required="">
</div>
<div class="form-group col-md-3">
<label for="">Age</label>
<input type="number" class="form-control" id="age" placeholder="age" name='Age' >
</div>
<div class="form-group col-md-2">
<label for="">Image</label>
<input type="file" class="btn btn-success form-control" name="img">
</div>
<div class="form-group col-md-1">
<label for=""> Save</label>
<button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save</button>
</div>
</div>
</form>
CONTROLLER:
function save() {
$this->Reg_model->save();
redirect(base_url('Reg_form/index'), 'refresh');
}
MODEL:
public function save() {
// load base_url
$this->load->helper('url');
// Check form submit or not
$data = array();
// Set preference
$config['upload_path'] = './assets/images';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '100'; // max_size in kb
$config['file_name'] = $_FILES['img']['name'];
//Load upload library
$this->load->library('upload',$config);
// File upload
if($this->upload->do_upload('img')){
// Get data about the file
$uploadData = $this->upload->data();
$file_name = $uploadData['file_name'];
$data['response'] = 'successfully uploaded '.$filename;
}else{
$data['response'] = 'failed';
}
$data = array(
'Name' => $this->input->post('Fname'),
'Fname' => $this->input->post('Lname'),
'age' => $this->input->post('Age'),
'image' => $file_name
);
$this->db->insert('student',$data);
}
When I tried to upload the image it says undefined variable $file_name.
enter image description here this is the image of codeigniter showing the error
I am having a problem uploading an image file to a destination folder in my CodeIgniter application and it is not working at all. I have referred so many articles and downloaded some working one but they wouldn't work once in my application
echo $this->upload->display_errors();in theelse, that should show you why it's not uploading.if($this->upload->do_upload('img'))- if this condition isn't fulfilled, you are not assigning a value to$file_nameanywhere, yet after this you have code that tries to access that variable in either case. Why are you letting this code proceed any further, after you have determined that the upload failed? If you want to create a new student record either way, even if they did not upload a photo - then you need to leave'image' => $file_nameout of your$dataarray, or set it to NULL or something.$data['response']values that you are assigning in the if- and else-branch respectively, of course get wiped out when you overwrite$datawith a completely new array afterwards.