Reputation: 1
Hi guys I am new to Codeigniter and new to the MVC webdesign, and I've been trying to implement this image upload into my registration form with no success.
In another page where the user edits his profile i've added it successfully but for the life of me i can't add it to this register form. It's not uploading to my database or to my server
Here is what I've tried:
//upload controller
public function do_upload() {
$user = $this->ion_auth->user()->row();
$config['upload_path'] = './some/folder/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 2000;
$config['overwrite'] = TRUE;
$current_user_id = $user->id;
$current_user_username = $user->username;
$config['file_name'] = $current_user_id . "_" . $current_user_username;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
redirect('/', 'refresh');
}
else {
$vars['user'] = $user;
$current_user = $user->id;
$vars['recent_messages'] = $this->ss_messages_model->find_all_array(array('id_user' => $user->id), 'date_added desc', 5);
$image_data = $this->upload->data();
$data = array('userfile' => $image_data['file_name']);
$this->ion_auth->update($user->id, $data);
}
}
//register function in the auth controller
function register()
{
$this->data['title'] = "Register User";
$this->data['lang'] = $this->clang;
**$this->do_upload();**
$tables = $this->config->item('tables','ion_auth');
//validate form input
$this->form_validation->set_rules('first_name', $this->lang->line('register_fname'), 'alpha_space|required|xss_clean');
$this->form_validation->set_rules('last_name', $this->lang->line('register_user_validation_lname_label'), 'alpha_space|required|xss_clean');
if ($this->form_validation->run() == true)
{
$username = strtolower($this->input->post('first_name')) . ' ' . strtolower($this->input->post('last_name'));
$email = strtolower($this->input->post('email'));
$password = $this->input->post('password');
$additional_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
**'userfile' => $this->input->post('userfile');**
);
}
if ($this->form_validation->run() == true && $this->ion_auth->register($username, $password, $email, $additional_data))
{
//check to see if we are creating the user
//redirect them back to the admin page
$this->session->set_flashdata('message', $this->ion_auth->messages());
$this->_render_page("auth/_success");
}
else
{
//display the register user form
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
$this->data['image']= $this->_create_captcha();
$this->_render_page('auth/register', $this->data);
}
}
**//view**
<div class="input-box">
<div class="agent-lable"><?php echo "Picture: (jpg or png)"; ?>
</div><br>
<input type = "file" name = "userfile" size = "20" />
</div>
Upvotes: 0
Views: 1624
Reputation: 9265
You need to realize a few things:
do_upload
the user isn't created yet. As you want to add an image for the user, you need to wait until the IonAuth register
function adds them to the database.$this->ion_auth->user()->row()
when the user id isn't specified gets the row of the current user: "If a user id is not passed the id of the currently logged in user will be used." As nobody is logged in yet $user->username
will be null and $user->id
in the update function will also be null - hence it is not updating; and if it did, it wouldn't be for the correct user!You should modifiy your register
function like so:
function register() {
echo 'register function triggered <br>';
$this->data['title'] = "Register User";
$this->data['lang'] = $this->clang;
//$this->do_upload(); User isn't created yet!!
//$tables = $this->config->item('tables', 'ion_auth');
//validate form input
$this->form_validation->set_rules('first_name', $this->lang->line('register_fname'), 'alpha_space|required|xss_clean');
$this->form_validation->set_rules('last_name', $this->lang->line('register_user_validation_lname_label'), 'alpha_space|required|xss_clean');
if ($this->form_validation->run()) {
echo 'form validation passed <br>';
$username = strtolower($this->input->post('first_name')) . ' ' . strtolower($this->input->post('last_name'));
$email = strtolower($this->input->post('email'));
$password = $this->input->post('password');
$additional_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
//'userfile' => $this->input->post('userfile') files are not in $_POST array
);
$register = $this->ion_auth->register($username, $password, $email, $additional_data);
if ($register) {
echo 'user successfully registered <br>';
print_r($register);
/**
* The user is already registered here... If the upload function fails, we should let
* them know somewhere else other than the register form as this hasn't affected
* their registration
*/
$upload = $this->do_upload($register);
if ($upload !== true) {
$this->session->set_flashdata('message', $upload);
}
$this->session->set_flashdata('message', $this->ion_auth->messages());
$this->_render_page("auth/_success");
} else {
$this->data['message'] = $this->ion_auth->errors();
$this->data['image'] = $this->_create_captcha();
$this->_render_page('auth/register', $this->data);
}
} else {
$this->data['message'] = validation_errors();
$this->data['image'] = $this->_create_captcha();
$this->_render_page('auth/register', $this->data);
}
}
and your do_upload
function to accept the newly created user id as a parameter:
public function do_upload($uid) {
// added new users uid
$user = $this->ion_auth->user($uid)->row();
$config['upload_path'] = './some/folder/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = 2000;
$config['overwrite'] = TRUE;
$current_user_id = $user->id;
$current_user_username = $user->username;
$config['file_name'] = $current_user_id . "_" . $current_user_username;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
echo '<br> upload errors <br>';
exit($this->upload->display_errors());
return $this->upload->display_errors();
//$error = array('error' => $this->upload->display_errors());
//redirect('/', 'refresh');
} else {
echo 'upload success <br>';
// variables unused in scope
//$vars['user'] = $user;
//$current_user = $user->id;
//$vars['recent_messages'] = $this->ss_messages_model->find_all_array(array('id_user' => $user->id), 'date_added desc', 5);
$image_data = $this->upload->data();
$data = array('userfile' => $image_data['file_name']);
$this->ion_auth->update($user->id, $data);
return true;
}
}
It is important to note. Even if for some reason the upload function fails, we shouldn't re-show the register form... the user is already registered (remember, we needed their id). Instead redirect them to the success page and just say hey, we couldn't upload your profile picture.
If your profile picture is required I would suggest to validate that it exists before continuing with the register function, so like before form validation.
If you didn't want the username and user id to for the image name there is another option than this code that is cleaner.
Upvotes: 1