不允许上传类PDF文件类型


CodeIgniter Upload class PDF filetype not allowed

在本地工作,但在我尝试过的两个服务器上显示相同的错误消息。使用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类型在配置文件config/mimes.php

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

编辑:如果这意味着什么,本地服务器是MAC,两个远程服务器是windows。

所以即使代码都是正确的错误实际上是在PHP本身。这里有个拼写错误。当var_dump($_FILES)输出["type"]=> string(14) "aplication/pdf"时注意"application"拼写错误。

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

由于某些原因,我在这里遇到了同样的问题,我无法上传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中有一个错字:)

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

代替

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

Try this

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