php CodeIgniter:“不允许您尝试上传的文件类型。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9815208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:40:59  来源:igfitidea点击:

CodeIgniter: "The filetype you are attempting to upload is not allowed."

phpcodeignitermime-typescodeigniter-2

提问by StackOverflowNewbie

I'm experiencing a very odd upload problem. Here's the relevant view file:

我遇到了一个非常奇怪的上传问题。这是相关的视图文件:

<form action="http://localhost/index.php/temp/upload/" method="post" enctype="multipart/form-data">
    <fieldset>
        <input type="file" name="userfile"/>
        <input type="submit" value="Upload"/>
    </fieldset>
</form>

And here's my tempcontroller's upload()method:

这是我的temp控制器的upload()方法:

public function upload()
{
    $config['upload_path']   = FCPATH . 'uploads' . DIRECTORY_SEPARATOR;
    assert(file_exists($config['upload_path']) === TRUE);
    $config['allowed_types'] = 'avi|mpg|mpeg|wmv|jpg';
    $config['max_size']      = '0';

    $this->load->library('upload', $config);
    if ($this->upload->do_upload('userfile') === FALSE)
    {
        // Some error occured
        var_dump($this->upload->display_errors('', ''));
        var_dump($_FILES);
    }
    else
    {
        // Upload successful
        var_dump($this->upload->data());
    }
}

When I upload an AVI video, everything works fine. When I upload, say, a WMV video, I get the following var dumps:

当我上传 AVI 视频时,一切正常。例如,当我上传 WMV 视频时,我得到以下 var 转储:

string 'The filetype you are attempting to upload is not allowed.' (length=57)

array
  'userfile' => 
    array
      'name' => string 'wmv.wmv' (length=7)
      'type' => string 'video/x-ms-wmv' (length=14)
      'tmp_name' => string 'C:\wamp\tmp\php2333.tmp' (length=23)
      'error' => int 0
      'size' => int 83914

The "wmv" extension is being interpreted as the MIME type: video/x-ms-wmv. This should be fine since my config/mimes.php has the following:

“wmv”扩展名被解释为 MIME 类型:video/x-ms-wmv. 这应该没问题,因为我的 config/mimes.php 有以下内容:

'wmv' =>  array('video/x-ms-wmv', 'audio/x-ms-wmv')

It's a similar situation when I try uploading other files. So far, the only one that seems to work is my test AVI video.

当我尝试上传其他文件时,情况类似。到目前为止,似乎唯一可行的是我的测试 AVI 视频。

Any ideas what might be wrong?

任何想法可能有什么问题?

UPDATE 1:

更新1:

One my machine, only AVI uploads. On another developer's machine, no files upload. On yet another developer's machine, all supported files upload. Are these browser or server issues?

一台我的机器,只能上传AVI。在另一个开发人员的机器上,没有文件上传。在另一台开发人员的机器上,所有支持的文件都上传。这些是浏览器还是服务器问题?

回答by Rocco

You are using Firefox, aren't you?

您正在使用 Firefox,对吗?

You could try looking at system/libraries/Upload.phpline 199:

您可以尝试查看system/libraries/Upload.php第 199 行:

$this->_file_mime_type($_FILES[$field]);

Change that line to:

将该行更改为:

$this->_file_mime_type($_FILES[$field]); var_dump($this->file_type); die();

Then do upload your .wmvfile. It would show something like application/octet-streamor whatever. Add that to your mimes.php. Hope this help =)

然后上传你的.wmv文件。它会显示类似的application/octet-stream东西。将其添加到您的mimes.php. 希望这有帮助 =)

Similar answer here

类似的答案在这里

Some links:

一些链接:

回答by Kanchi

$config["allowed_types"] ="*";

$config["allowed_types"] ="*";

回答by P M

  1. Upload your .wmv file to the server using FTP/SFTP/scp/etc where you run the CodeIgniter application
  2. On the server where you have uploaded the .wmv file, execute the following PHP code

    <?php
    $file_path = 'CHANGE_THIS_TO_ABSOLUTE_FILE_PATH_THAT_YOU_HAVE_UPLOADED.wmv';
    $finfo = finfo_open(FILEINFO_MIME);
    $mime = finfo_file($finfo, $file_path);
    var_dump($mime);
    ?>
    
  3. Save the code as mime.php

  4. Run in the terminal - $ php mime.php
  5. If it dumps any other value than you already have for wmv, append that value into wmv mime types.
  1. 使用 FTP/SFTP/scp/etc 将您的 .wmv 文件上传到您运行 CodeIgniter 应用程序的服务器
  2. 在你上传 .wmv 文件的服务器上,执行以下 PHP 代码

    <?php
    $file_path = 'CHANGE_THIS_TO_ABSOLUTE_FILE_PATH_THAT_YOU_HAVE_UPLOADED.wmv';
    $finfo = finfo_open(FILEINFO_MIME);
    $mime = finfo_file($finfo, $file_path);
    var_dump($mime);
    ?>
    
  3. 将代码另存为 mime.php

  4. 在终端中运行 - $ php mime.php
  5. 如果它转储除 wmv 已有的任何其他值,请将该值附加到 wmv mime 类型中。

Also check PHP_VERSION in other development machines. finfo_open is introduced in PHP 5.3. The development machine where upload is working may have an older version of PHP or it may have right mime type for the wmv.

还要检查其他开发机器中的 PHP_VERSION。finfo_open 是在 PHP 5.3 中引入的。上传工作的开发机器可能有旧版本的 PHP,或者它可能有适合 wmv 的 mime 类型。

回答by Anil

Answer for to Upload .doc file in CodeIgniter

在 CodeIgniter 中上传 .doc 文件的答案

Change this

改变这个

'doc'   =>  'application/msword',

With this on line no 95 (application/config/mimes.phpfile)

有了这个第 95 行(application/config/mimes.php文件)

'doc'   =>  array('application/vnd.ms-word','application/msword'),

回答by Taha Paksu

did you try using mime types instead of extensions in $config['allowed_types']?

您是否尝试在 $config['allowed_types'] 中使用 mime 类型而不是扩展名?

write it like this

像这样写

$config["allowed_types"] = "video/x-msvideo|image/jpeg|video/mpeg|video/x-ms-wmv";

回答by Shahid Hussain Aali

Check your mimes.php file in application/config. It should return an array of mime types e.g return $mimes;at end of file or return array(.....in start of mimes array.

检查 application/config 中的 mimes.php 文件。它应该返回一个 mime 类型数组,例如return $mimes;在文件末尾或return array(.....mimes 数组的开头。

回答by envysea

I've recently had some very similar problems with the Codeigniter's Upload Class.

我最近在 Codeigniter 的上传类中遇到了一些非常相似的问题。

The *allowed_types* doesn't seem to be working. For example, I wanted to allow .PNG images to be uploaded, but it wouldn't allow them through it's filter. I ended up investigating it further. I allowed all file types to be temporarily uploaded, I uploaded a .PNG, and then dumped the upload data ($this->upload->data());). For some reason, it thought the MIME type was text/plain! This might be related to your problem.

*allowed_types* 似乎不起作用。例如,我想允许上传 .PNG 图像,但不允许它们通过它的过滤器。我最终进一步调查了它。我允许临时上传所有文件类型,我上传了一个.PNG,然后转储了上传数据($this->upload->data());)。出于某种原因,它认为 MIME 类型是text/plain!这可能与您的问题有关。

There are some solutions to this I found surfing some forums where you can modify/extend the class or core, but they didn't work for me -- sorry. I believe it's a Codeigniter Core bug (I think the issue has already been opened with EllisLabs). I ended up hard-coding the damn thing anyways! Well, I hope this helps you some.

有一些解决方案,我在一些论坛上找到了一些可以修改/扩展类或核心的论坛,但它们对我不起作用 - 抱歉。我相信这是一个 Codeigniter Core 错误(我认为 EllisLabs 已经打开了这个问题)。无论如何,我最终对这该死的东西进行了硬编码!嗯,我希望这对你有所帮助。

Basic example/work-around,

基本示例/解决方法,

//replace with your allowed MIME types
if ($_FILES['name_of_form_upload']['type'] != 'image/jpeg' && $_FILES['name_of_form_upload']['type'] != 'image/png' && $_FILES['name_of_form_upload']['type'] != 'image/gif') {
    $data['message'] = '<div class="message">That file type is not allowed!</div>';
    $this->load->view('home_view', $data);
} else {
    //run upload code
}

Edit: Formatting/Grammar

编辑:格式/语法

回答by thedjaney

This is actually a bug in the core.. I upgraded to latest version of CI and the bug disappeared.

这实际上是核心中的一个错误。我升级到最新版本的 CI 并且错误消失了。

回答by Abhishek

if you need the simple way to solve this,there is an easy solution for it. open "system/libraries/Upload.php" file

如果您需要简单的方法来解决这个问题,有一个简单的解决方案。打开“系统/库/Upload.php”文件

line no 465
you will see 
if ( ! $this->is_allowed_filetype())
        {
            $this->set_error('upload_invalid_filetype', 'debug');
            return FALSE;
        }


just make it true

    if ( ! $this->is_allowed_filetype())
            {
                $this->set_error('upload_invalid_filetype', 'debug');
                return TRUE;
            }

note : it will stop your all file type validation.

回答by Anand

Change IN application\config\Mimes.php

更改 IN application\config\Mimes.php