CodeIgniter上传类不允许使用PDF文件类型

时间:2013-04-12 02:35:22

标签: php codeigniter file-upload mime-types

在本地工作但在两台服务器上我尝试过显示相同的错误消息。使用Codeigniter 2.1.3

private function upload_file(){
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'jpg|png|jpeg|gif|pdf';
    $config['max_width']  = '0';
    $config['max_height']  = '0';
    $config['encrypt_name']  = true;
    $this->load->library('upload', $config);
    var_dump($_FILES);
    if ( ! $this->upload->do_upload()){
        $error = array('error' => $this->upload->display_errors());
        var_dump($error);
        die();
        return $error;
    } else {
        $data = array('upload_data' => $this->upload->data());
        var_dump($data);
        die();
        return $data;
    }
}

在执行var_dump($_FILES);时,它会显示正确的信息 array(1) { ["userfile"]=> array(5) { ["name"]=> string(8) "0002.pdf" ["type"]=> string(14) "aplication/pdf" ["tmp_name"]=> string(27) "C:\Windows\Temp\php9454.tmp" ["error"]=> int(0) ["size"]=> int(29295) } }

var_dump($error)放弃array(1) { ["error"]=> string(64) " The filetype you are attempting to upload is not allowed. " }

使用png和jpg进行测试,这些工作非常出色。

正确的mime-types在配置文件config/mimes.php

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

编辑:如果它意味着什么,本地服务器是MAC,两个遥控器是窗口。

3 个答案:

答案 0 :(得分:2)

因此即使代码完全正确,错误实际上也是PHP本身。那里的mime-types存在拼写错误。 当var_dump($_FILES)吐出["type"]=> string(14) "aplication/pdf"时请注意“申请”拼写错误。

检查同事机器和他的正确,所以可能是php> 5.3.5的问题

答案 1 :(得分:0)

我在这里遇到了同样的问题,因为某些原因我无法上传pdf文件快速故障排除显示错误

$msg = $this->upload->display_errors('<p>', '</p>');
echo $msg;

我收到了无效文件类型的错误消息,因此我添加了一行代码以显示有关要上传的文件的完整信息。

$msg = $this->upload->display_errors('<p>', '</p>');
$msg.=print_r($this->upload->data());
echo $msg;

然后我将文件类型复制到config / mime.php

'pdf'   =>  array('application/pdf'),

并确保使用相同的mime类型作为我上传文件的mime类型。 有趣的是,错误是在mime.php中有一个错字:)

撇号而不是单引号,因为从互联网上复制而不注意使用的引号。

答案 2 :(得分:0)

而不是

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

试试这个

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

这对我有用

相关问题