简体   繁体   English

CodeIgniter文件上传类-初始化不同的配置文件

[英]CodeIgniter File Upload Class - Initialize different config file

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. 据我了解,这段代码: $this->upload->initialize()使用upload.php配置文件初始化CI文件上载类。 What I want to do is use a different file. 我想做的是使用其他文件。

I tried $this->upload->initialize('upload_other') , but that doesn't seem to work. 我尝试了$this->upload->initialize('upload_other') ,但这似乎不起作用。 I know you can just set a $config array in the controller, but I'm trying to avoid that. 我知道您可以在控制器中设置一个$config数组,但是我试图避免这种情况。

Is this possible? 这可能吗? Am I approaching this the wrong way? 我是以错误的方式接近这个吗?

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 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. 这样,您可以重复使用相同的设置。

Why are you trying to avoid the use of a config-array? 您为什么要尝试避免使用配置数组? The other way is to create the upload.php-config file. 另一种方法是创建upload.php-config文件。 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. 如果要跨不同的控制器使用不同的配置,则始终可以创建并加载完整的自定义配置文件: Codeigniter用户指南在这里,您可以使用不同的upload-config数组创建多个变量。

You can load this config-file in every controller and use these configurations by using the initialize method. 您可以在每个控制器中加载此配置文件,并通过使用initialize方法使用这些配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM