Welcome Guest, Not a member yet? Register   Sign In
Quotes in mime type when uploading
#11

[eluser]darkhouse[/eluser]
This has been bugging me for a while, so I decided to look into it further. Turns out there are a couple of issues. First, I believe it's a bug with the CI upload library in that if you're allowing image and non-image filetypes and then try uploading a non-image, the is_allowed_filetype method fails prematurely as it checks to see if one of the allowed filetypes is an image and then fails on getimagesize when the uploaded file is not an image. Essentially if you're allowing jpg's and doc's, and upload a doc, it will fail because it expects the file to be a valid image, which of course it is not.

The second issue is the firefox bug that adds quotes to certain mime types.

I made another little mod to allow all filetypes, essentially skipping the is_allowed_filetype method altogether.

Here's the code:
Code:
class MY_Upload extends CI_Upload {
    
    function is_allowed_filetype()
    {
        if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
        {
            $this->set_error('upload_no_file_types');
            return FALSE;
        }
        
        if(in_array('*', $this->allowed_types)) return TRUE; //allow all
        
        $this->file_type = str_replace(array('"', "'"), '', $this->file_type); //fix firefox mime type bug

        $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
        
        foreach ($this->allowed_types as $val)
        {
            $mime = $this->mimes_types(strtolower($val));
            
            // Images get some additional checks
            if (in_array($val, $image_types))
            {
                if (getimagesize($this->file_temp) !== FALSE) //check to see if it's not false and return true
                {
                    return TRUE;
                }
            }

            if (is_array($mime))
            {
                if (in_array($this->file_type, $mime, TRUE))
                {
                    return TRUE;
                }
            }
            else
            {
                if ($mime == $this->file_type)
                {
                    return TRUE;
                }
            }        
        }
        
        return FALSE;
    }
    
}

Now, another thing that was bugging me was why you could grab all of the keys from the mimes array and doing this would work:
Code:
$config['allowed_types'] = "hqx|cpt|csv|etc...";
but this would fail:
Code:
include(APPPATH.'config/mimes'.EXT);
$config['allowed_types'] = implode('|', array_keys($mimes));
unset($mimes);

Turns out that since you already included the mimes file, when is_allowed_filetype calls the mime_types function, it will not include the file again as it's doing a require_once, so it just returns false for each allowed filetype. The solution is to set the mimes array in the upload library at the same time, like this:
Code:
if(count($this->upload->mimes) == 0){ //check to see if the mime types haven't already been included
    if (@require_once(APPPATH.'config/mimes'.EXT)){
        $config['mimes'] = $mimes;
        unset($mimes);
    }
} else { //otherwise re-initialize the mime types
    $config['mimes'] = $this->upload->mimes;
}
$config['allowed_types'] = implode('|', array_keys($config['mimes']));

Or, if you want to simply allow all filetypes, just do this:
Code:
$config['allowed_types'] = '*';

And there you have it. A couple quick fixes and no more issues (...yet).




Theme © iAndrew 2016 - Forum software by © MyBB