conmen
conmen

Reputation: 2417

Codeigniter validate_upload library can't upload file

I was referred to @raheel shan's thread on this page and use this My_Upload extended library to have if(!$this->upload->validate_upload('photo')) validation of input file, the validation is work fine if user try to upload invalid file type and the message is just shown in the right place.

Somehow, I can't upload image file to its folder and the filename is unable to save into table, there was works pretty to upload the image file and saved to table before I use this library.

Can someone please help with figure what's wrong in my code?

Controller:

public function index()
{
    if(!$this->tank_auth->is_logged_in()) {
        redirect('/auth/login');

    }else{

        $user_id = $this->session->userdata('user_id');
        $profile = $this->users->get_profile_by_id($user_id);
        $checked = $this->input->post('remove');

        $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|min_length[5]|max_length[30]');
        $this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean');
        $this->form_validation->set_rules('website', 'Website', 'trim|xss_clean|max_length[30]');


        if(isset($_FILES['photo']) AND !empty($_FILES['photo']['name'])) {

            $this->form_validation->set_rules('photo', 'Photo', 'trim|callback_valid_upload');

            if($this->upload->do_upload('photo')){

                // image resizing
                $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 100;
                $config['height'] = 100;

                $this->load->library('image_lib', $config); //codeigniter default function

                if(!$this->image_lib->resize()){

                    $this->session->set_flashdata('upload_msg', $this->image_lib->display_errors());

                }else{

                    $image_data = $this->upload->data();
                    $photo_to_table = $image_data['file_name'];
                }

            }

        }else{

            if((int) $checked == 1){

                unlink('./uploads/'.$profile->photo);
                $photo_to_table = '';

            }else{
                $photo_to_table = $profile->photo;
            }
            //$photo_to_table = ((int) $checked == 1) ? '' : $profile->photo;
        }

        if($this->form_validation->run()) { // validation ok

            if(!is_null($data = $this->tank_auth->update_user(
                $this->form_validation->set_value('name'),
                $this->form_validation->set_value('country'),
                $this->form_validation->set_value('website'),
                $photo_to_table
            ))) { // success

                $this->session->set_flashdata('msg', 'Profile has been updated');
                redirect(current_url());
            }

        }else{

            $errors = $this->tank_auth->get_error_message();
            foreach($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v);
        }

//blah blah..//load view

}

public function valid_upload()
{
    $user_id = $this->session->userdata('user_id');

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size'] = '500';
    $config['file_name'] = $user_id.'_'.strtotime('now');
    $config['overwrite'] = TRUE;
    $config['remove_spaces'] = TRUE;

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

    if(!$this->upload->validate_upload('photo'))
    {
        $this->form_validation->set_message('valid_upload', $this->upload->display_errors());
        return FALSE;
    }else{
        return TRUE;
    }
}


 }

Thanks.

Upvotes: 0

Views: 615

Answers (2)

Polder
Polder

Reputation: 121

What is your result in

$this->image_lib->display_errors()

Is your upload directory writable?

Do you have GD installed and active in php? Maybe try GD as Mahmoud suggested?

Upvotes: 0

M. Tahan
M. Tahan

Reputation: 111

I had a similar problem and in my case it was because I chose the wrong image library. Check your PHP installation for the appropriate image library you wish to use and make sure it matches. By default, CodeIgniter chooses GD2 (GD library versions >= 2) as the default image library.
http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

Try this:

// image resizing
$config['image_library'] = 'gd'; // specify the correct image library
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;

Upvotes: 1

Related Questions