phpcodeignitercodeigniter-3form-submitzend-form-element

Codeigniter file upload from a form without ajax/jquery/javascript


Context : I am updating user_profile data after user registration using form #updateprofileform , i am able to process all other form fields using standard validations and insert/update using user_model. issue is with resume field which should accept a file as input type and upload to server and store its path on server (say http://domain/uploads/$filename) to a column in database.

Expectation A logged in user , able to view his details(if already in database),update details if incomplete profile.

Question : I found methods how we can upload a file to server using CI framework or how we can upload a file in this context using jquery/ajax/java-script. But i am looking for simple solution for above problem which can do the job with out dependency on technologies.

user_profile.php (view)

<table>
<form class="row" name="updateprofile" action = "<?php echo base_url()?>user/user_profile" method="POST">
<tr>
        <td><label for="email">Email ID</label></td><td><input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php if (isset($email)) echo $email;?>" /> <span style="color:red"><?php  echo form_error('email'); ?></span></td>
    </tr>
  <tr>
        <td><label for="resume">Resume</label></td>
        <td>
          <input name="resume" placeholder="Upload resume" type="file" value="<?php if (isset($resume)) echo $resume;?>" />
          <span style="color:red"><?php  echo form_error('resume'); ?>
          </span>
        </td>
    </tr>
  <tr>
        <td></td>
        <td><div class="col-md-6 col-lg-3"> <button type="submit" class="btn btn--primary type--uppercase" name="updateprofile" value="updateprofile" formaction = "<?php echo base_url()?>user/user_profile">Update Profile</button> </div></td>
    </tr>
  </form>
</table>

user_controller

class User extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
$config = array(
        'upload_path' => "./uploads/",
        'allowed_types' => "doc|docx|pdf",
        'overwrite' => TRUE,
        'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
        //'max_height' => "768",
        //'max_width' => "1024"
        );
        $this->load->library('upload', $config);
    }
  public function user_profile()
    {  
      $resume = $this->input->post('resume');
  if ($this->user_model->set_user_profile($id,FALSE) )
            { if($this->do_upload($resume)){
                $this->session->set_flashdata('msg_success','Updation Successful!');
                  echo validation_errors();}
            }
            else
            {
                $this->session->set_flashdata('msg_error','Error! Please try again later.');
                redirect('user/user_profile');
            }
  public function do_upload($resume){

    if($this->upload->do_upload($resume))
    {
    $data = array('upload_data' => $this->upload->data($resume));

    $this->load->view('user_profile',$data);
    }
    else
    {
    $error = array('error' => $this->upload->display_errors($resume));
    $this->load->view('user_profile', $error);
    }
    }

currently always error.... Error! Please try again later . so appreciate some pointers here to upload both file and form data together and save file location in database without ajax/jquery.

Thanks


Solution

  • Resolved ! 4 important steps needed and some missing in most Q&A in all other posts to make this complete.will share samplecode in github when i get sometime.

    1. form encryption type must be enctype="multipart/form-data" (thanks to @Javier Larroulet and @dexter )
    2. do_upload method by default look for file field with name=userfile (Thanks to @Alex ! for referencing this), so, if you have your form's file field name different than userfile, then only it is required to mention as do_upload('filefieldname') otherwise optional
    3. loading of upload library configuration is tricky.. if you autoupload the upload library , then load->library upload config will fail!, you just =>initialize it.$this->upload->initialize($config); if you are not using autoload for upload library, then yes, you can load it from controller. $this->load->library('upload', $config);
    4. when ever, you are using upload, you have to catch the form validation errors and data errors separately (again thanks to @Alex for pointing this)

    a nice documentation is given from codeigniter at this link (https://www.codeigniter.com/userguide3/libraries/file_uploading.html#the-controller ) but doesn't highlight routine mistakes.

    hope this post help someone not to waste another 24 hours to debug.