kamal
kamal

Reputation: 1546

why pdf upload fails on codeigniter 1.7.2?

i have same problem. other files doc, xls uploads fine. but pdf uploading gives error. “The filetype you are attempting to upload is not allowed.” in config/mimnes.php i have:

'pdf'    =>    array('application/pdf', 'application/x-download', 'application/download'),  

in controllers i have:

function upload_file($type, $upload_type)
{
    $this->load->library('upload');
    //upload file
    switch($upload_type){
        case "image":
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|jpg|png|pdf';
            $config['max_size'] = '3000';
            $config['max_width'] = '3224';
            $config['max_height'] = '1268';
        break;
        case "doc":
            $config['upload_path'] = './uploads/pages/doc/';
            $config['allowed_types'] = 'pdf|doc|docx|xls|ppt';
            $config['max_size'] = '3000';
            $config['encrypt_name'] = TRUE;
        break;
    }
    foreach($_FILES as $key => $value)
    {
        if( ! empty($value['name']))
        {
                $this->upload->initialize($config);

                if ( ! $this->upload->do_upload($key))
                {
                $errors = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata('flashError', $errors);

                }
                else
                {
                     $this->page_model->process_file($type, $upload_type);
                }
         }
    }
}  

any help will be appreciable.

Upvotes: 0

Views: 3135

Answers (3)

webicy
webicy

Reputation: 1624

I had the same issue as well but figured out that some PDF files are sent as type application/octet-stream in FireFox. I don't know why. But try to open mimetype.php add it to the mimetype array of pdf file like this :

'pdf' => array('application/pdf', 'application/x-download', 'application/binary', 'application/octet-stream'),

Upvotes: 1

Carlos Mora
Carlos Mora

Reputation: 1184

Are you using FireFox? I had the same problem, originated by FF. The mimetype reported is application/binary, so you can chage that in mimes.php

'pdf'   =>  array('application/pdf', 'application/x-download', 'application/binary'),

that should solve the problem.

Upvotes: 0

Sarwar Erfan
Sarwar Erfan

Reputation: 18068

Remove this line:

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

Then add this line after end of switch block and before foreach block

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

You are loading the upload library and then setting config values, which has no effect.

Your modified function should look like this:

function upload_file($type, $upload_type)
{
    //upload file
    switch($upload_type){
        case "image":
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|jpg|png|pdf';
            $config['max_size'] = '3000';
            $config['max_width'] = '3224';
            $config['max_height'] = '1268';
        break;
        case "doc":
            $config['upload_path'] = './uploads/pages/doc/';
            $config['allowed_types'] = 'pdf|doc|docx|xls|ppt';
            $config['max_size'] = '3000';
            $config['encrypt_name'] = TRUE;
        break;
    }

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

    foreach($_FILES as $key => $value)
    {
        if( ! empty($value['name']))
        {
                $this->upload->initialize($config);

                if ( ! $this->upload->do_upload($key))
                {
                $errors = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata('flashError', $errors);

                }
                else
                {
                     $this->page_model->process_file($type, $upload_type);
                }
         }
    }
} 

Upvotes: 0

Related Questions