CodeIgniter Forums
Updating file using CI File Upload Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Updating file using CI File Upload Library (/thread-53772.html)



Updating file using CI File Upload Library - El Forum - 08-08-2012

[eluser]jamesduncan[/eluser]
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.

http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

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.

Thanks!

Code:
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);
  }
  else
  {
   $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);
  }
}




Updating file using CI File Upload Library - El Forum - 08-08-2012

[eluser]Aken[/eluser]
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.


Updating file using CI File Upload Library - El Forum - 08-08-2012

[eluser]jamesduncan[/eluser]
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!


Updating file using CI File Upload Library - El Forum - 08-08-2012

[eluser]Aken[/eluser]
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).


Updating file using CI File Upload Library - El Forum - 08-08-2012

[eluser]jamesduncan[/eluser]
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

http://ellislab.com/forums/viewthread/106972/


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