繁体   English   中英

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

UPDATE

您可以在配置文件中指定常规数据。

config['width'] = '100';

config['width2'] = '100';

现在在您的控制器中使用

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

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

这样,您可以重复使用相同的设置。

您为什么要尝试避免使用配置数组? 另一种方法是创建upload.php-config文件。 如果要跨不同的控制器使用不同的配置,则始终可以创建并加载完整的自定义配置文件: Codeigniter用户指南在这里,您可以使用不同的upload-config数组创建多个变量。

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

暂无
暂无

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

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