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

标签 php codeigniter

所以我一直在谷歌搜索这个问题,似乎找不到答案。

据我所知,这段代码:$this->upload->initialize() 使用 upload.php 配置文件初始化 CI 文件上传类。我想要做的是使用不同的文件。

我尝试了 $this->upload->initialize('upload_other'),但这似乎不起作用。我知道您可以在 Controller 中设置一个 $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';

现在在你的 Controller 中使用

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

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

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

关于php - CodeIgniter 文件上传类 - 初始化不同的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8062527/

相关文章:

php - 如何使用 php 清理特定标签的 textarea 输入

php - CakePHP - 导航到页面时的空白页面

php - 如何在 CodeIgniter 中执行 SQL 查询

Codeigniter 和 $_SERVER

php - 没有任何键的多维数组值打印

php - 有没有办法将 json 数组发送到服务器端 php 并将其值插入表中?

php - CakePHP:创建 View 计数

php - 如何使用 CI 事件记录将数据插入带空格的字段名称?

php - 设置现有 CodeIgniter 时出现数据库连接错误

php - Dynamodb 中的排序 SCAN 操作