Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Codeigniter中上载-不允许尝试上载的文件类型_Php_Codeigniter - Fatal编程技术网

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

Php 在Codeigniter中上载-不允许尝试上载的文件类型,php,codeigniter,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-&

我得到一个错误:当我尝试上载任何文件时,不允许您尝试上载的文件类型

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和jpeg,每次都会出现相同的错误

变量转储($\u文件);给我:

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'),

我在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中,您可以按照此处的教程允许所有文件类型:)

如果您使用的是Codeigniter 2.1.0版,则上载库中存在错误。有关更多详细信息,请参阅

基本上,我所做的是修改File Upload类中的几行代码(位置:./system/libraries/Upload.php)

1) 修改行号1044

发件人:

为此:

$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return; 
@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_name']), $output, $return_code); 
2) 修改线号1058

发件人:

为此:

$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return; 
@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_name']), $output, $return_code); 

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

我也有同样的问题。您可能需要检查应用程序是否识别正在上载的文件的mimetype。将新的mimetype添加到config/mimes.php修复了此问题。:-)

解决方案是用CodeIgniter v2.0.3的Upload.php替换system/libraries/by Upload.php中的Upload.php

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

这使我能够使用标准的CI语法,并避免对原始文件进行黑客攻击!我的库被自动使用,因为它只是简单地“扩展”了原始文件,这是一个完全无痛的解决方案,如果出于某种原因您必须替换原始文件,它不会崩溃


PS:当我想生成自定义日志时,我也可以使用Logging类来实现这一点。

这个问题是由于没有PHP FileInfo扩展造成的。upload类使用的函数是该扩展的一部分


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

  • 打开
    system/libraries/Upload.php
    (方法
    do_Upload()
    第205行)并执行此操作

    var_dump($this->file_type);
    exit();
    
  • 尝试上传一些文件

  • 将您在
    var_dump()
    中看到的文件类型添加到
    application/config/mimes.php

  • 小例子:您在
    .docx
    方面有问题。尝试添加:

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

    这是针对Codeigniter 2.2版的。如果这个问题仍然相关。我将文件系统/libraries/upload.php文件中的故障跟踪到函数:
    受保护函数\u file\u mime\u type($file)
    第1032行和第1046行:
    $this->file\u type=$matches[1]
    当我上传一个扩展名为.txt的文件时,第1046行的语句似乎给
    $this->file\u type
    分配了一个不正确的值
    'text/x-asm'
    ,该值后来与
    'text/plain'
    进行比较,并且由于mime类型与测试不匹配,测试失败并发出不正确的文件类型和错误消息:

    '不允许您尝试上载的文件类型'


    解决方案,不确定,但快速修复似乎有效,将第1032行的条件更改为
    ,这样它就会显示:
    如果(!function_存在('finfo_文件'))
    而不是:
    如果(function_存在('finfo_文件'))
    。希望这能帮助到其他人。

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


    所以在下面,我已经描述了解决这个问题和解决方案的真正方法

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

    新mimes.php

    返回数组

    旧mimes.php

    $mimes=数组

    如果仍然使用旧版本的mimes.php,那么Common.php函数&get_mimes()将返回一个空数组。 这具有破坏Upload.php的敲门效应

    一旦我追踪到这一点,一切都正常:)

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

    试着使用
    $this->upload->data()
    检查CodeIgniter读取的dile信息,很可能你在那里找到了一些线索。我觉得还好吗?-<代码>数组(14){[“文件名”]=>string(15)“minifur-hs1.jpg”[“文件类型”]=>string(10)“图像/jpeg”[“文件路径”]=>string(32)”D:/www/website/uploads/invoices/“[“完整路径”]=>string(47)“D:/www/website/uploads/invoices/minifur-hs1.jpg”[“原始名称”=>string(11)“minifur-hs1”[“原始名称”=>string(0)”=>string(15)”“minifur-hs1.jpg”[“文件外部”]=>string(4)”.jpg“[“文件大小”=>int(18168)[“是图像”]=>bool(真)[“图像宽度”]=>string(0)”[“图像高度”]=>string(0)”[“图像类型”]=>string(0)”[“图像大小”=>string(0)”}
    我也遇到了这个问题。我在表单的另一个字段中使用“.”时出现了错误,但每当我不使用“.”时,它似乎工作得很好。奇怪的是+1我总是遇到这个问题。有时有效,有时无效。@dangermark——web服务器是否运行PHP v5.2?升级到最新版本后,我也遇到了同样的问题st CodeIgniter,这就是我的问题。降级到CodeIgniter v2.0.3解决了它。在我找到有效的解决方案之前,这是唯一的解决方案