Welcome Guest, Not a member yet? Register   Sign In
set_allowed_types() in Upload library. Mistery?
#1

[eluser]Ignacio[/eluser]
Hi! I'm super hacking the Upload library, and I found that function. That function is doing something, but I can't find where is the call of it.

Anyone can give me a tip? Thanks!
#2

[eluser]richthegeek[/eluser]
Just did a recursive search on the whole CI installation, and it only occurs within system/libraries/Upload.php

It adds an item (or items?) to the system/config/mimes.php array.
#3

[eluser]Ignacio[/eluser]
The function make an array with the values from $config['allowed_types'] to compare with the mimes array.

config here:
Code:
$config['allowed_types'] = 'gif|jpg|png';

the MAGIC function here:
Code:
function set_allowed_types($types)
{
    $this->allowed_types = explode('|', $types);
}

for here:
Code:
foreach ($this->allowed_types as $val)
{
    $mime = $this->mimes_types(strtolower($val));

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

I can't understand how set_allowed_types() is it working and not calling it anywhere.
#4

[eluser]richthegeek[/eluser]
check in the constructor?
#5

[eluser]richthegeek[/eluser]
check lines 108-115 in the Upload.php file - the function is called by the variabled $method.

The method_exists() function checks all CI instance methods to see if there is a match, I guess, although im not sure 100% how this works.

Whilst is set_allowed_types() is never called explictly (that is, a search will never show it up) as long as that function exists, it will work.

To be honest itst just lazy and clever coding on the part of the dev.
#6

[eluser]Ignacio[/eluser]
found it!

Code:
foreach ($defaults as $key => $val)
{
    if (isset($config[$key]))
    {
        $method = 'set_'.$key; //$key = allowed_types = set_allowed_types()
        if (method_exists($this, $method))
        {
            $this->$method($config[$key]);
        }
        else
        {
            $this->$key = $config[$key];
        }            
    }
    else
    {
        $this->$key = $val;
    }
}

Now I get how this work! Thanks!
#7

[eluser]Ignacio[/eluser]
Yes, we found it at the same time. Thanks!
I didn't get what you mean with "To be honest itst just lazy and clever coding on the part of the dev."

Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB