Ali
Ali

Reputation: 267077

How to make CodeIgniter file upload class accept all extensions?

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?

Upvotes: 25

Views: 56015

Answers (8)

Wingpilot
Wingpilot

Reputation: 11

$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.php in "application/config" folder. Just include your mime types in array.

Example of adding epub and fb2 file types:

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.

Upvotes: 0

Mehta Harshit
Mehta Harshit

Reputation: 140

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.

Upvotes: 1

Shafeer khan
Shafeer khan

Reputation: 502

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

Upvotes: 1

Alana Storm
Alana Storm

Reputation: 166066

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.

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

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.

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

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 changelog whenever you're upgrading, but this will allow you to keep your code and the core code in separate universes.

Upvotes: 4

Baris
Baris

Reputation: 696

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

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

Upvotes: 68

Sarfraz
Sarfraz

Reputation: 382726

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

Upvotes: 0

Adam
Adam

Reputation: 41

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.

Upvotes: 4

Ali
Ali

Reputation: 267077

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

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

Upvotes: 2

Related Questions