在Codeigniter中上传 - 不允许您尝试上传的文件类型

dan*_*ark 16 php codeigniter

我收到错误:当我尝试上传任何文件时,不允许您尝试上传的文件类型.

if(!empty($_FILES['proof_of_purchase']['name'])) {
    $config['upload_path'] = './uploads/invoices/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|bmp';
    $config['max_size'] = '3000';
    $this->load->library('upload', $config);

      // if there was an error, return and display it
    if (!$this->upload->do_upload('proof_of_purchase'))
    {
        $data['error'] = $this->upload->display_errors();
        $data['include'] = 'pages/classic-register';
    } else {
        $data['upload_data'] = $this->upload->data();
        $filename = $data['upload_data']['file_name'];
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试过很多不同的文件 - 主要是gif和jpeg,每次都会得到同样的错误.

后续代码var_dump($ _ FILES); 给我:

array(1) { ["proof_of_purchase"]=> array(5) { ["name"]=> string(28) "2010-12-04_00019.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(19) "D:\temp\php2BAE.tmp" ["error"]=> int(0) ["size"]=> int(58054) } } 
Run Code Online (Sandbox Code Playgroud)

我检查了mime配置,它包含正确的东西.例:

'jpeg'  =>  array('image/jpeg', 'image/pjpeg'),
'jpg'   =>  array('image/jpeg', 'image/pjpeg'),
'jpe'   =>  array('image/jpeg', 'image/pjpeg'),
Run Code Online (Sandbox Code Playgroud)

我花了很长时间才开始这么做,这让我疯了!任何想法都会非常有帮助.

小智 25

如果您使用的是Codeigniter 2.1.0版,则上传库中存在错误.有关详细信息,请参见http://codeigniter.com/forums/viewthread/204725/.

基本上我所做的是修改文件上传类中的几行代码(位置:./ system/library/Upload.php)

1)修改行号1044

从:

$this->file_type = @mime_content_type($file['tmp_name']);
return;
Run Code Online (Sandbox Code Playgroud)

对此:

$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return; 
Run Code Online (Sandbox Code Playgroud)

2)修改行号1058

从:

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_path']), $output, $return_code);
Run Code Online (Sandbox Code Playgroud)

对此:

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_name']), $output, $return_code); 
Run Code Online (Sandbox Code Playgroud)

正如您可能看到的那样,第1058行尝试使用不存在的数组值.


swa*_*ins 5

我在CI中遇到了同样的问题,并且无法在论坛或谷歌上找到解决方案.我所做的是允许所有文件类型,以便上传文件.然后,我手动处理逻辑以确定是允许/保留文件,还是删除它并告诉用户不允许文件类型.

$config['upload_path'] = './uploads/invoices/';
$config['allowed_types'] = '*'; // add the asterisk instead of extensions
$config['max_size'] = '3000';
$this->load->library('upload', $config);

if (!$this->upload->do_upload('proof_of_purchase'))
{
    $data['error'] = $this->upload->display_errors();
    $data['include'] = 'pages/classic-register';
} else {
    $data['upload_data'] = $this->upload->data();
    // use custom function to determine if filetype is allowed
    if (allow_file_type($data['upload_data']['file_ext'])) 
    {
        $filename = $data['upload_data']['file_name'];
    }
    else
    {
        show_error('File type is not allowed!');
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 - 假设您正在使用CI 2(在CI 1中,您可以按照此处的教程允许所有文件类型:http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-大师/)