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


Uploading in Codeigniter - The filetype you are attempting to upload is not allowed

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

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'];
    }
}

我试过很多不同的文件,大部分是gif和amp;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) } } 

我已经检查了mime配置,它包含了正确的内容。示例:

'jpeg'  =>  array('image/jpeg', 'image/pjpeg'),
'jpg'   =>  array('image/jpeg', 'image/pjpeg'),
'jpe'   =>  array('image/jpeg', 'image/pjpeg'),

如果您使用的是Codeigniter 2.1.0版本,则Upload库中存在错误。看见http://codeigniter.com/forums/viewthread/204725/了解更多详细信息。

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

1( 修改线路编号1044

来自:

$this->file_type = @mime_content_type($file['tmp_name']);
return;

到此:

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

2( 修改行号1058

来自:

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

到此:

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

正如您可能看到的,第1058行试图使用一个不存在的数组值。

我在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!');
    }
}

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

我所做的是创建自己的库"my_Upload"来扩展CI_Upload类,然后我只是复制了CI_Uplod类,并在我的自定义库中应用了Adam概述的更改(感谢BTW提供的解决方案(。

这使我可以使用标准的CI语法,并避免黑客攻击原始文件!我的库是自动使用的,因为它只是"扩展"了原始文件,这是一个完全无痛的解决方案,如果出于某种原因必须替换原始文件,它不会中断。

PS:当我想生成自定义日志时,我也用Logging类来做这件事。

我也遇到了同样的问题。您可能需要检查应用程序是否识别正在上传的文件的mimetype。在config/mimes.php中添加一个新的mimetype修复了这个问题。:-(

这适用于Codeigniter版本2.2。如果这个问题仍然相关。我在文件系统/librarys/upload.php文件中追踪到函数:protected function _file_mime_type($file)的第1032行和第1046行:$this->file_type = $matches[1];当我上传一个扩展名为.txt的文件时,第1046行的语句似乎给$this->file_type分配了一个不正确的'text/x-asm'值,后来将其与'text/plain'进行了比较,由于mime类型不匹配,测试失败,并发出不正确的文件类型和错误消息:

"不允许您尝试上传的文件类型">

解决方案,不确定,但似乎有效的快速修复,将行1032的条件更改为NOT,使其读取为:if (!function_exists('finfo_file'))而不是:if (function_exists('finfo_file'))。希望这能帮助到别人。

1(允许所有文件类型。2( 使用传统php手动设置验证规则以接受或拒绝文件类型。3( 如果遵守验证规则,则使用CI上载助手进行上载。

if (isset($_FILES['upload_file']) && !empty($_FILES['upload_file'] ['name'])) {
        $file_name=$_FILES['upload_file'] ['name'];
        $acceptedext=array("zip","pdf","png","jpg","jpeg");
        $a=(explode('.', $file_name));
        $b=end($a);
        $file_ext=strtolower($b);
        if (!in_array($file_ext, $acceptedext)) {
            $this->session->set_flashdata( 'flash_message_error', "file format not accepted!" );
            redirect( base_url( 'page' ) );
        }
        else{
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = '*';

            $this->load->library('upload');
            $this->upload->initialize($config);
            if (!$this->upload->do_upload('upload_file')) {
                $error = $this->upload->display_errors('', ' ');
                $this->session->set_flashdata( 'flash_message_error', $error );
                redirect( base_url( 'page' ) );
            } } }

解决方案是用CodeIgniter v2.0.3 的Upload.php替换系统/库中的Upload.php

此问题是由于没有php FileInfo扩展而引起的。上传类使用的函数是该扩展的一部分。

http://www.php.net/manual/en/ref.fileinfo.php

如果您不想更改系统文件。试试这个:

  1. 打开system/libraries/Upload.php(方法do_upload()第205行(并执行此

    var_dump($this->file_type);
    exit();
    
  2. 尝试上载一些文件。

  3. 添加文件类型,您可以在var_dump()application/config/mimes.php中看到该类型。

小例子:您对.docx有问题。尝试添加:

'docx' => array('application/zip', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')

application/config/mimes.php

可能值得检查一下您是否拥有最新版本的application/config/mimies.php,因为它现在只返回一个数组,而不是设置变量$mimes。

mimes.php

返回阵列

mimes.php

$mimes=阵列

如果你仍然有旧版本的mimes.php,那么Common.php函数&get_mimes((返回一个空数组。这具有破坏Upload.php 的连锁反应

一旦我追踪到这一点,一切都很好:(

当我试图上传一个上传表单时,我遇到了同样的问题,&";。xlsx";文件,在CodeIgniter的mimes.php文件的数组中有一个条目,表示";xlsx";文件扩展名。

因此,在下面的链接中,我描述了解决这个问题和找出解决方案的真正步骤!