-1

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

3
  • Try adding echo $this->upload->display_errors(); in the else, that should show you why it's not uploading. Commented Jan 17 at 9:58
  • 2
    if($this->upload->do_upload('img')) - if this condition isn't fulfilled, you are not assigning a value to $file_name anywhere, 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_name out of your $data array, or set it to NULL or something. Commented Jan 17 at 10:57
  • 1
    And both of your $data['response'] values that you are assigning in the if- and else-branch respectively, of course get wiped out when you overwrite $data with a completely new array afterwards. Commented Jan 17 at 10:58

1 Answer 1

0

Replace Your Code in Model `save()` Function

if($this->upload->do_upload('img')){
  // Get data about the file
  $uploadData = $this->upload->data();
  $file_name = $uploadData['file_name'];
  $data['response'] = 'successfully uploaded '.$file_name;
}else{
  $data['response'] = 'failed';
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.