0

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

5
  • 1
    You don't ever need JavaScript, jQuery or anything else to use the Codeigniter upload library, as long as your web server, upload directory and PHP code are all well written. I know, I do uploads the old fashioned way :) What you must do, and is probably the reason why you're getting errors is set your form as multipart/form-data and handle both parts separately on your backend (with $this->upload for the file and $this->input->post() for the form fields) Commented Dec 2, 2018 at 13:41
  • 1
    this is problematic logic. your main issue is missing multipart as javier and the answer suggested, but doing if($this->upload(... is weird. just make it all one controller function. further you are confusing form_error() and validation_errors() with display_errors() the first two are methods are from the form validation library and aren't part of the upload library and thus won't work as you expect them to work. Commented Dec 2, 2018 at 15:55
  • @JavierLarroulet i think i am missing that link of passing file data and catching properly in controller,upload ,on success grab url and store to database. at present, using $this->input-post($filenametag) in controller which giving me error..You did not select file to uploadbut on application profiling i see the posted file data. some example code is much appreciated.Thanks Commented Dec 2, 2018 at 17:08
  • @Alex Thanks, got your point on errors, now catching them in two different views(user_profile.php and displayerrors.php) to watch those errors. Commented Dec 2, 2018 at 17:12
  • 1
    fyi do_upload automatically looks for an upload field with the name userfile, yours is resume do it should be do_upload('resume') passing a $_post field has nothing to do with the $_files array (where files reside). Commented Dec 3, 2018 at 0:09

2 Answers 2

0

Change the form definition to the following:

<form class="row" action = "<?php echo base_url('user/user_profile'); ?>" method="post" enctype="multipart/form-data" name="updateprofile" >
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dexter! thats changed,but this one is not the final. i am getting error as "You did not select a file to upload." but when did application profiling ..i see file is posting to controller...as $_POST['updateprofile'] updateprofile $_FILES['resume'] Array ( [name] => 28112017_NNNN.pdf [type] => application/pdf [tmp_name] => C:\xampp\tmp\php5777.tmp [error] => 0 [size] => 821163 ) basically am looking for one example code where we upload some file and data then catch the filepath on server and store database table.
0

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.

Comments

Your Answer

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