2

So I've been Googling this for a bit, can't seem to find an answer.

From what I understand, this code: $this->upload->initialize() initializes the CI file upload class using the upload.php config file. What I want to do is use a different file.

I tried $this->upload->initialize('upload_other'), but that doesn't seem to work. I know you can just set a $config array in the controller, but I'm trying to avoid that.

Is this possible? Am I approaching this the wrong way?

rgin
  • 2,291
  • 4
  • 24
  • 32

2 Answers2

7

You can not Initialize / override configurations like that.

You can initialize by

$this->config->load('upload');
-- Some code Here -- 

$this->config->load('upload_other');
-- Some code Here -- 

OR you can do it by array as follows.

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

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

// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);

If you want to have anouther upload at same time you can change your config array.

$config2['upload_path'] = './uploads/small/';
$config2['allowed_types'] = 'gif|jpg|png';
$config2['max_size'] = '100';
$config2['max_width'] = '100';
$config2['max_height'] = '100';

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

// Alternately you can set
$this->upload->initialize($config2);

UPDATE

you can specify your general data in config file. say

config['width'] = '100';

config['width2'] = '100';

Now use in your controller like

config['width'] = $this->config->item('width');

config2['width'] = $this->config->item('width2');

this way you can reuse same settings.

pinaldesai
  • 1,835
  • 2
  • 13
  • 22
  • so, this is not possible at all? that sucks, having to put a config array on the controller itself. What if I wanted to use the same configurations on a different form? Do I have to declare this config array again? – rgin Nov 09 '11 at 09:27
  • seems there's really no real answer to my question out there. I appreciate the help folks. Marking this as the answer till someone figures out a better one. – rgin Nov 09 '11 at 10:33
0

Why are you trying to avoid the use of a config-array? The other way is to create the upload.php-config file. If you want to use diferent configurations accross different controllers, you can always create and load a complete custom config-file: Codeigniter userguide Here you can create multiple variables with differen upload-config arrays.

You can load this config-file in every controller and use these configurations by using the initialize method.

Stijn_d
  • 1,078
  • 9
  • 20
  • I've tried that. It doesn't work. `$this->load->config($config)` returns a boolean value, which I cannot use in `$this->upload->initialize()`. the only way for that to work is to load the config file and fetch each item one by one, which is basically the same as declaring the `$config` array in the controller. – rgin Nov 09 '11 at 09:29
  • You can just store an array in the config value: `$config['upload_settings_1'] = array('upload_path' => './uploads/', 'allowed_types' => 'jpeg|', ).....` – Stijn_d Nov 14 '11 at 08:21