meow meow - 1 month ago 10
PHP Question

codigniter: always throw You did not select a file to upload. not able to update

Hello i need help below is my code

It doesn't update any think except the avatar always display error You did not select a file to upload.
don't know how to solve it please help

if($this->input->post()){
$this->form_validation->set_rules('avatar','avatar','callback_multiple_image_upload|trim');
$this->form_validation->set_rules('overview','overview','trim');
$this->form_validation->set_rules('history','history','trim');
$this->form_validation->set_rules('education','education','trim');
$this->form_validation->set_rules('joining','joining','trim');
$this->form_validation->set_rules('hobbies','hobbies','trim');
$this->form_validation->set_rules('experience','experience','trim');
$this->form_validation->set_rules('skills','skills','trim');

/* if($this->input->post('avatar')){
$this->form_validation->set_rules('avatar','avatar','callback_multiple_image_upload|trim');
}else{} */
if($this->form_validation->run())
{
// check if avatar is uploaded
$images = ($this->session->userdata('uploaded_images'))?$this->session->userdata('uploaded_images'):array();
if($this->session->userdata('uploaded_avatar')){
$user['avatar'] = $this->session->userdata('uploaded_avatar');
$this->session->unset_userdata('uploaded_avatar');
}

$data = array(
'avatar' => ($images)?$images[0]['file_name']:'',
'thumb' => ($images)?$images[0]['thumb']:'',
'overview' =>$this->input->post('overview'),
'history' =>$this->input->post('history'),
'education' =>$this->input->post('education'),
'joining' =>$this->input->post('joining'),
'hobbies' =>$this->input->post('hobbies'),
'experience' =>$this->input->post('experience'),
'skills' =>$this->input->post('skills'),


);
$abc = $this->user_model->update($user_id,$data,'user_id');
$this->session->set_flashdata('success','User updated successfully');
redirect('admin/staff/edit/'.$user_id);

}
}



public function multiple_image_upload()
{
$config = array();
$config['upload_path'] = './uploads/user_avatars/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$config['max_width'] = 10240;
$config['max_height'] = 7680;

//$images = array();

if(isset($_FILES['avatar']) && ($count = count($_FILES['avatar']['name'])) > 0)
{
$files = $_FILES;
$images = array();
/* check if folder with year exists*/
$current_year = date('Y');
$path = './uploads/user_avatars/'.$current_year;

if(is_dir($path)){
/* do nothing */
}else{
/* create directory */
@mkdir( $path , 0755, true );
}

$images = array();
for($i = 0; $i < $count; $i++)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100000';
$config['max_width'] = '102400';
$config['max_height'] = '76800';
$this->load->library('upload', $config);

if ($this->upload->do_upload('avatar'))
{
$data = $this->upload->data();

/* create thumbnail */
$this->load->helper('image_thumb_helper');
generate_image_thumb($data['full_path'],$path,$data['file_name']);

/* add to database */
$images[$i]['file_name'] = $current_year.'/'.$data['file_name'];
$images[$i]['thumb'] = $current_year.'/thumb_'.$data['file_name'];
}else{
$this->form_validation->set_message('multiple_image_upload',$this->upload->display_errors());
return false;
}
}

$this->session->set_userdata('uploaded_images',$images);
return true;
}
else{

return true;
}
}


please provide me solution m not able to remove this error :(

Answer

I thing you are using multifile upload. please try bellow one for "for loop"

for($i = 0; $i < $count; $i++) 
    {

         $_FILES['avatar_solo']['name'] = $_FILES['avatar']['name'][$i];
            $_FILES['avatar_solo']['type'] = $_FILES['avatar']['type'][$i];
            $_FILES['avatar_solo']['tmp_name'] = $_FILES['avatar']['tmp_name'][$i];
            $_FILES['avatar_solo']['error'] = $_FILES['avatar']['error'][$i];
            $_FILES['avatar_solo']['size'] = $_FILES['avatar']['size'][$i];

        $config['upload_path'] = $path;
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size'] = '100000';
        $config['max_width']  = '102400';
        $config['max_height']  = '76800';
        $this->load->library('upload', $config);

        if ($this->upload->do_upload('avatar_solo'))
        {
            $data = $this->upload->data();

            /* create thumbnail */
            $this->load->helper('image_thumb_helper');
            generate_image_thumb($data['full_path'],$path,$data['file_name']);

            /* add to database */                   
            $images[$i]['file_name'] = $current_year.'/'.$data['file_name'];
            $images[$i]['thumb'] = $current_year.'/thumb_'.$data['file_name'];
        }else{
            $this->form_validation->set_message('multiple_image_upload',$this->upload->display_errors());
            return false; 
        }    
    }