Jame Value
Jame Value

Reputation: 33

Cannot upload image in CodeIgniter

I cannot upload image in Codeigniter.

It always show The filetype you are attempting to upload is not allowed.

My coding (Controller):

$this->load->helper(array('form', 'url'));
            $this->load->library('upload');
            $config['upload_path'] = "./public/uploadimg";
            $config['allowed_types'] = "*";
            $config['max_size'] = '10000';

            $this->upload->initialize($config);

            if ( ! $this->upload->do_upload('userfile'))
            {
                echo $this->upload->display_errors();
                //$this->load->view('upload_form', $error);
            }
            else
            {
                echo $this->upload->data($config);
                //$this->load->view('upload_success', $data);
            }

View :

<form  enctype="multipart/form-data" action="http://192.1.100.1/site/test_image/upload/" method="post">
    <input type="file"  name="userfile" id="userfile"/><Br>
    <input type="submit" name="upload" id="upload">
</form>

Regards,

Upvotes: 0

Views: 1192

Answers (2)

Thanh Nguyen
Thanh Nguyen

Reputation: 5352

        $this->load->helper(array('form', 'url')); 
       if($this->input->post('upload')){ //submit name           

        $config['upload_path'] = "./public/uploadimg";
        $config['allowed_types'] = "png|jpg|jpeg";
        $config['max_size'] = '10000';
        $config['max_width'] = '1000';
        $config['max_height'] = '800';

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

        if ( ! $this->upload->do_upload('userfile')){
            echo $this->upload->display_errors();
            //$this->load->view('upload_form', $error);
            }
        else{
            echo $this->upload->data($config);
            //$this->load->view('upload_success', $data);
            }
        }

And make sure that your action url is right. sorry i'm bad at English.

Upvotes: 0

Errol Fitzgerald
Errol Fitzgerald

Reputation: 3008

Try setting your allowed types explicitly. I remember having the same problem before.

$config['allowed_types'] = 'docx|pdf|doc|gif|jpg|png|tiff'; 

Upvotes: 1

Related Questions