0

I'm trying to create a user profile edit form on a Codeigniter project with the ability to upload a profile image. I'd like allow the upload to go through when the user clicks the submit button. I have no problems with the form (no errors), but I do not see any image on the uploads folder I set up. Any help to get the upload working would be greatly appreciated.

Controller (User Edit/Insert Values to Database)

public function update_edit($id) 
 {   

     $config['upload_path'] = '/uploads/profilepics';
     $config['allowed_types'] = 'gif|jpg|png|jpeg';
     $config['max_size'] = 250;
     $this->load->library('upload', $config);
     $this->upload->do_upload('profile_img_path');
     $data_upload_files = $this->upload->data();

     $image = $data_upload_files[full_path]; 
 //input data
 $data = array(
   'user_name' => $this->input->post('user_name'), 
   'email' => $this->input->post('email'),
   'first_name' => $this->input->post('first_name'), 
   'last_name' => $this->input->post('last_name'),
   'city' => $this->input->post('city'), 
   'country' => $this->input->post('country'),
   'description' => $this->input->post('description'),
   'date_edited' => $this->input->post('today'),
   'profile_img_path' => $image 
 ); 
 $this->user_model->update_account($id,$data); 

 //send a confirmation of user account update
 redirect('/');     

}

Controller to access the form

public function update_account()  

 { 

  $id = $this->uri->segment(3); 
  $data['user_profile'] = $this->user_model->get_user_profile($id);
  //only profile owner can access
  if (!$this->uri->segment(3))
   { 
     redirect('404'); 
   } 


  if ($this->is_logged_in()) 
  {  
    if ($this->is_logged_user()) 
    { 

   //update views
   $this->load->view('components/top'); 
   $this->load->view('interact/form/user/edit',$data);  
   $this->load->view('components/bottom'); 
    } 
   else 
    { 
     redirect('403'); 
    } 
  } 
  else 
  { 
    redirect('403');  
  } 
 } 

The View

   <div id="page">  
     <div class="container"> 

       <div class="row"> 

    <h3>Edit Your Account</h3>


   <?php foreach ($user_profile as $user): ?>


<form id="edit_form" class="form-horizontal" method="post" action="<?php echo base_url() . "users/update_edit/" . $user->user_id; ?>" enctype="multipart/form-data">

    <label>Enter a new username: *</label>
    <p>yloko.com/users/user_profile/<?php echo $user->user_name; ?>  </p>
    <br/> 
    <input type="text" name="user_name" value="<?php echo $user->user_name; ?>">     

     <label >Enter a new email*</label>
    <input type="email" name="email" value="<?php echo $user->email; ?>" >

    <label >First Name</label>
    <input type="text" name="first_name" value="<?php echo $user->first_name; ?>" >

     <label >Last Name</label>
    <input type="text" name="last_name" value="<?php echo $user->last_name; ?>" >

     <label >City</label>
    <input type="text" name="city" value="<?php echo $user->city; ?>" >

     <label >Country</label>
    <input type="text" name="country" value="<?php echo $user->country; ?>" >


    <label >Description</label>     
    <textarea name="description"><?php echo $user->description; ?></textarea>

     <!--INPUT-->
     <?php echo $user->profile_img_path; ?>
     <input name = "profile_img_path" type="file" class="input-xlarge" id = "profile_img_path" />

    <!--submit button-->    
    <input type="hidden" name="today" value="<?php echo date('Y-m-d H:i:s'); ?>" >   
    <input type="submit" value="Update">     

</form> 
 <?php endforeach; ?> 
  <a href="<?=site_url('/')?>">Back</a>
  <br/> 

    </div> 
    </div>
  </div>
1
  • 1
    $config['upload_path'] = './uploads/profilepics'; verify this, you should create the uploads folder with the root directory and the form tag has to have attribute enctype="multipart/form-data" Commented Feb 14, 2015 at 10:39

3 Answers 3

1

Can you try this code after library is being loaded.

$this->upload->initialize($config);
if (!$this->upload->do_upload('profile_img_path')) {
    $error = array('error' => $this->upload->display_errors());
    echo $error['error'];
}

Let us know what exact error you are getting.

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I'm getting: Message: Use of undefined constant full_path - assumed 'full_path' Filename: controllers/users.php Line Number: 290. Looks like I need to put the single quote markers for the full_path.
This helped me point the error. Just made the fix and it's working properly now. Thanks for the help.
1

You should include enctype in form

enctype="multipart/form-data"
<form id="edit_form" class="form-horizontal" method="post" action="<?php echo base_url() . "users/update_edit/" . $user->user_id; ?>" enctype="multipart/form-data"> 

2 Comments

Forgot to put that part in. However, when I tried it out it still doesn't work.
@JeffreyTeruel could you please update your post then accordingly?
0

$config['upload_path'] = '/uploads/profilepics'; points to a folder in the filesystem, which you probably have no write access to. Use a relative path like $config['upload_path'] = './uploads/profilepics'; (./ being the current directory). Also make sure the folder(s) exist and you have write access to it.

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.