php 如何让 CodeIgniter 文件上传类接受所有扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/968196/
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-25 00:31:46  来源:igfitidea点击:

How to make CodeIgniter file upload class accept all extensions?

phpcodeigniter

提问by Click Upvote

Currently if I supply no extensions to the class it allows no extensions. I would like to allow all extensions. Is there any way to do this without hacking the core?

目前,如果我不提供类的扩展,则不允许扩展。我想允许所有扩展。有没有办法在不破解核心的情况下做到这一点?

回答by Baris

In Codeigniter 2, you simply need to define allowed types like this :

在 Codeigniter 2 中,您只需要像这样定义允许的类型:

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

回答by Alan Storm

The answer to your direct question: No, there's no way to do this without overriding the core

您直接问题的答案:不,如果不覆盖核心,就无法做到这一点

To good news is you can avoid hacking the core, per the manual

好消息是你可以避免破解核心,根据手册

As an added bonus, CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries folder.

作为额外的好处,如果您只需要向现有库添加一些功能,CodeIgniter 允许您的库扩展本机类。或者,您甚至可以通过在 application/libraries 文件夹中放置相同名称的版本来替换本机库。

So, to have a drop in replacement for your library, you could copy Upload.php to your

因此,要替换您的库,您可以将 Upload.php 复制到您的

application/libraries

folder, and then add your custom logic to that Upload.php file. Code Igniter will include this file instead whenever you load the upload library.

文件夹,然后将您的自定义逻辑添加到该 Upload.php 文件中。每当您加载上传库时,Code Igniter 都会包含此文件。

Alternately, you could create your OWN custom uploader class that extends the original, and only refines the is_allowed_filetype function.

或者,您可以创建扩展原始文件的 OWN 自定义上传器类,并且仅改进 is_allowed_filetype 函数。

application/libraries/MY_Upload.php
class MY_Upload Extends CI_Upload{
    function is_allowed_filetype(){
         //my custom code here
    }
}

You'll want to read over the changelogwhenever you're upgrading, but this will allow you to keep your code and the core code in separate universes.

无论何时升级,您都需要通读更改日志,但这将允许您将代码和核心代码保存在不同的宇宙中。

回答by Adam

What I do is:

我要做的是:

$ext=preg_replace("/.*\.([^.]+)$/","\1", $_FILES['userfile']['name']);
$fileType=$_FILES['userfile']['type'];
$config['allowed_types'] = $ext.'|'.$fileType;

That makes all files in every function call automatically allowed.

这使得自动允许每个函数调用中的所有文件。

回答by Click Upvote

So far it looks like it would only be possible via a hack.

到目前为止,它看起来只能通过 hack 来实现。

I inserted a return trueon line 556 in system/libraries/Upload.php.

return true在 system/libraries/Upload.php 的第 556 行插入了一个。

回答by Shafeer khan

$config['allowed_types'] = '*';Which Will Upload Any File Formats Like .exe or .jpegs extra...

$config['allowed_types'] = '*';这将上传任何文件格式,如 .exe 或 .jpegs 额外...

回答by Mehta Harshit

if all not works then rearrange the allowed type order like that in first video format

如果一切都不起作用,则重新排列允许的类型顺序,如第一个视频格式

$config['allowed_types'] = 'mp4|jpg|png|'

It will works in my case so I shared if possible try it.

它适用于我的情况,所以我分享了如果可能的话尝试一下。

回答by Wingpilot

$config['allowed_types'] = '*';is possible solution, but IMHO it is not very safe. The better way is to add a file types you need to $config['allowed_types']. In case the CodeIgniter doesn't have the MIME types you need, you can add them by editing file mimes.phpin "application/config" folder. Just include your mime types in array.

$config['allowed_types'] = '*';是可能的解决方案,但恕我直言,它不是很安全。更好的方法是将您需要的文件类型添加到$config['allowed_types']。如果 CodeIgniter 没有您需要的 MIME 类型,您可以通过编辑“application/config”文件夹中的mimes.php文件来添加它们。只需在数组中包含您的 mime 类型。

Example of adding epub and fb2 file types:

添加 epub 和 fb2 文件类型的示例:

return array(
'epub'  =>  'application/epub+zip',
'fb2'   =>  'application/x-fictionbook+xml',
'hqx'   =>  array('application/mac-binhex40', 'application/mac-binhex', 'application/x-binhex40', 'application/x-mac-binhex40'),

Then you'll be able to use the extensions you added in $config['allowed_types']variable.

然后您将能够使用您在$config['allowed_types']变量中添加的扩展。

回答by Sarfraz

You simply need to replace this condition:

您只需要替换此条件:

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

With:

和:

        if (count($this->allowed_types) && !$this->is_allowed_filetype())
        {
            $this->set_error('upload_invalid_filetype');
            return false;
        }