adaxa
adaxa

Reputation: 1608

codeigniter upload config

How to specify more than one configuration in config/upload.php ?

Upvotes: 0

Views: 3452

Answers (2)

Rob Lambell
Rob Lambell

Reputation: 96

Here's another way.

application/config/upload.php

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
    'member_photo' => array(
        'upload_path'   => './uploads/member_photos/',
        'allowed_types' => 'gif|jpeg|jpg|png',
        'max_size'      => '0',
        'max_width'     => '0',
        'max_height'    => '0',
        'encrypt_name'  => true
    ),
    'pet_photo' => array(
        'upload_path'   => './uploads/pet_photos/',
        'allowed_types' => 'gif|jpeg|jpg|png',
        'max_size'      => '0',
        'max_width'     => '0',
        'max_height'    => '0',
        'encrypt_name'  => true
    ),
);

application/libraries/MY_Upload.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Upload extends CI_Upload
{

    function initialize($config = array())
    {
        // load config
        if(!empty($config['config']))
        {
            $CI =& get_instance();
            $CI->load->config('upload');
            $autoload_config = $CI->config->item($config['config']);

            if($autoload_config)
            {
                foreach($autoload_config as $key => $val)
                {
                    if(empty($config[$key]))
                    {
                        $config[$key] = $val;
                    }
                }
            }

            unset($config['config']);
        }

        parent::initialize($config);
    }

}

Then in your controller; any extra keys you define will override those in the config file:

$this->load->library('upload', array('config' => 'member_photo'));

Upvotes: 0

ipalaus
ipalaus

Reputation: 2273

I think that it's not posible to do it, the manual says:

Setting preferences in a config file

If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the upload.php, add the $config array in that file. Then save the file in: config/upload.php and it will be used automatically. You will NOT need to use the $this->upload->initialize function if you save your preferences in a config file.

So you're adding to the $config array() without any key to auto-initialize. Probably will be better to make a config file and load it with your config params like:

$config['upload_1']['upload_path'] = './uploads/';
$config['upload_1']['allowed_types'] = 'gif|jpg|png';
$config['upload_1']['max_size'] = '100';
$config['upload_1']['max_width']  = '1024';
$config['upload_1']['max_height']  = '768';

And loading later in your Controller with:

$this->load->config('upload_vals', TRUE);

$upload_vals = $this->config->item('upload_1');

$this->load->library('upload', $upload_vals);

Wish it helps!

Upvotes: 4

Related Questions