为什么pdf upload在codeigniter 1.7.2上失败?

时间:2011-01-04 04:05:22

标签: codeigniter file-upload

我有同样的问题。其他文件doc,xls上传很好。但pdf上传会给出错误。 “不允许您尝试上传的文件类型。” 在config / mimnes.php中我有:

'pdf'    =>    array('application/pdf', 'application/x-download', 'application/download'),  
控制器中的

我有:

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);
                }
         }
    }
}  

任何帮助都会很明显。

3 个答案:

答案 0 :(得分:1)

我也有同样的问题,但发现一些PDF文件在FireFox中以 application / octet-stream 类型发送。我不知道为什么。但是尝试打开mimetype.php将它添加到pdf文件的mimetype数组中,如下所示:

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

答案 1 :(得分:0)

删除此行:

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

然后在切换块结束后和foreach块

之前添加此行
$this->load->library('upload', $config);

您正在加载上传库,然后设置配置值,这无效。

您修改后的功能应如下所示:

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);
                }
         }
    }
} 

答案 2 :(得分:0)

您使用的是FireFox吗?我有同样的问题,由FF发起。报告的mimetype是application / binary,所以你可以在mimes.php

中使用它
'pdf'   =>  array('application/pdf', 'application/x-download', 'application/binary'),

应解决问题。