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


CodeIgniter File Upload Class - Initialize different config file

我在谷歌上搜索了一会儿,似乎找不到答案。

根据我的理解,这段代码:$this->upload->initialize()使用upload.php配置文件初始化CI文件上传类。我要做的是使用另一个文件。

我试过$this->upload->initialize('upload_other'),但这似乎不起作用。我知道你可以在控制器中设置一个$config数组,但我试图避免这种情况。

这可能吗?我是不是说错了?

不能这样初始化/重写配置

可以通过

初始化
$this->config->load('upload');
-- Some code Here -- 
$this->config->load('upload_other');
-- Some code Here -- 

或者您可以按如下方式使用数组。

$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);

如果你想在同一时间有另一个上传,你可以改变你的配置数组。

$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);

你可以在配置文件中指定你的通用数据。说

config['width'] = '100';
config['width2'] = '100';

现在在你的控制器中使用

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

为什么要避免使用config-array?另一种方法是创建upload.php-config文件。如果你想在不同的控制器上使用不同的配置,你可以创建并加载一个完整的自定义配置文件:Codeigniter userguide在这里,你可以用不同的上传配置数组创建多个变量。

您可以在每个控制器中加载此配置文件,并通过使用initialize方法使用这些配置。