CodeIgniter Forums

Full Version: Updating file using CI File Upload Library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hi there,

I've searched the forum and tried to find some info on StackOverflow - but I might not be understanding things correctly.

I have successfully been able to upload files to my server and insert the file path to my my database using The File Uploader Library from the User Guide.

I'm using Ion Auth for my User Management (thanks for all your help Ben!) so I can also successfully insert the ID of the user that just uploaded the file into the Database.

But I can't figure out how to overwrite the existing file path once the user has uploaded another one.

Basically I'm just trying to build out a User Profile Image upload form - so I might be doing this all completely backward - but learning a lot in the process - so all good.

Here is my do_load function from the user guide mixed in with some Ion Auth code - the file is uploading ok - Im getting a success confirmation and the path is getting written to my database table - I now need to be able to tell if the user already has a profile image and over-write the existing one with a newly uploaded one - let know if any one needs to see any other code from me.


function do_upload()
  $user = $this->ion_auth->user()->row();
  //$user_id = $this->ion_auth->user()->row('id');
  //$user = $this->ion_auth->user()->row();
  //echo $user->id;
  //'userid' => $userid['userid'],
  $config['upload_path'] = './gs-assets/uploads/';
  $config['allowed_types'] = 'gif|jpg|jpeg|png';
  $config['max_size'] = '100';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';

  $this->load->library('upload', $config);

  if ( ! $this->upload->do_upload())
   $error = array('error' => $this->upload->display_errors());

   $this->load->view('upload_form', $error);
   $image_data = $this->upload->data();
   $insert_data = array(
      //'id' => $this->ion_auth->user()->row('id'),
      //'id' => $user,
      'id' => $user->id,
                    'name' => $image_data['file_name'],
                    'path' => $image_data['full_path'],
                    //'thumb_path'=> $image_data['file_path'] . 'thumbs/'. $image_data['file_name'],
                    //'tag' => $tag
           $this->db->insert('profile_photos', $insert_data);//load array to database
   $data = array('upload_data' => $this->upload->data());
   $this->load->view('upload_success', $data);

El Forum

Can you configure Ion Auth to include the profile_photos table joined in with the rest of the user info? That would be the best way, IMO, as it will save you an additional query. Then you'll already have an existing value that you can check when performing your upload.

El Forum

Ahh ok - I see - so Add a "profile_pic_url" to the users table then simply write to that - makes total sense.

Ok I'll work away on that and come back if I have more questions

Cheers and thanks much for the reply!

El Forum

Not necessarily that, but if you can add a join query to the method that pulls the user data to include the profile_photos table, you'll have the info. That's a better way if database normalization is important (which is good practice anyway).

El Forum

I see

But I'm still a bit lost

When I write to profile_photos I'm sorta creating

name - img_name.jpg
path - /path/to/my/img/img_name.jpg
id - 1

and when I re upload it creates this:

name - another_img_name.jpg
path - /path/to/my/img/another_img_name.jpg
id - 1

I'm looking at this thread on joining but they didnt create a model in the User Guide so I'm a touch lost - I'm still on the training wheels here lol

Sorry Im really new at this so I probably scoped this out incorrectly and really appreciate your input